简体   繁体   English

Xamarin.Android-使用SQLite.NET ORM创建多个数据库

[英]Xamarin.Android - Create Multiple Databases with SQLite.NET ORM

I had two databases in my app,which i created with the help of the SQLiteOpenHelper class.I decided to do away with the SQLiteOpenHelper and just create the databases using the SQLite component only.I have a databaseHandler class where i create the dbs and the tables.The problem is i can only seem to create one database.If i add a second database name to be created,i get a black screen on start up and then it crashes.It seems to me like the two dbs can't coexist in the same location.I'm not sure how to solve the issue though. 我的应用程序中有两个数据库,这些数据库是在SQLiteOpenHelper类的帮助下创建的。我决定取消使用SQLiteOpenHelper,仅使用SQLite组件创建数据库。问题是我似乎只能创建一个数据库。如果我添加要创建的第二个数据库名称,启动时会出现黑屏,然后崩溃。在我看来,这两个数据库不能共存在同一位置。我不确定如何解决该问题。

namespace com.Sample.Database
{
public class DatabaseHandler 
{

private string COMPANY_DATABASE_NAME;

private string USER_DATABASE_NAME = "User.db";
private string COMPANY_DATABASE_PATH;
private string USER_DATABASE_PATH;
private int databaseVersion = 3;
private SQLiteConnection companyDatabaseConnection;
private SQLiteConnection userDatabaseConnection;
private static DatabaseHandler dbHandler;
Context context;

//Constructor
private DatabaseHandler (Context context)
{
try
{
//this.COMPANY_DATABASE_NAME = "company" + "v" + this.databaseVersion + ".db";

    string dataDirectory = Environment.GetFolderPath (Environment.SpecialFolder.Personal);

     USER_DATABASE_PATH = System.IO.Path.Combine (dataDirectory, USER_DATABASE_NAME);

     COMPANY_DATABASE_PATH = System.IO.Path.Combine (dataDirectory, COMPANY_DATABASE_NAME);


    this.context = context;

}
catch(Exception ex)
{

            logTable.Exception (context, ex);

}


public static DatabaseHandler GetInstance(Context context)
   {
       if (dbHandler == null) {
             dbHandler = new DatabaseHandler (context);
      }

return dbHandler;
}
  public void initialize()
   {
       try
       {


    this.userDatabaseConnection = new SQLiteConnection(USER_DATABASE_PATH);

    this.userDatabaseConnection.CreateTable<UserTable> ();


    this.InsertData();

  //            this.companyDatabaseConnection = new SQLiteConnection     (COMPANY_DATABASE_PATH);
//
  //            this.companyDatabaseConnection .CreateTable<RecordsTable> ();

}
      catch(Exception e)
     {
                logTable.Exception (context, ex);


        }
   }

} }

The crashing happens when i uncomment the line where i'm assigning a name to the company database: 当我取消为公司数据库分配名称的行的注释时,发生崩溃:

//this.COMPANY_DATABASE_NAME = "company" + "v" + this.databaseVersion + ".db";

And gives me the following output: 并给我以下输出:

[Mono] Assembly Ref addref GTI.Thales.Droid[0x7be12eb8] ->  SQLite[0x7c389c38]: 2
[Mono] Assembly Ref addref SQLite[0x7c389c38] -> mscorlib[0x73ac7ca8]: 10
[Mono] GC_OLD_BRIDGE num-objects 0 num_hash_entries 0 sccs size 0 init 0.00ms df1 0.00ms sort 0.00ms dfs2 0.00ms setup-cb 0.00ms free-data 0.00ms links   0/0/0/0 dfs passes 0/0
[Mono] GC_MINOR: (Nursery full) pause 147.43ms, total 147.98ms, bridge 0.34ms promoted 448K major 688K los 0K
[Mono] GC_OLD_BRIDGE num-objects 0 num_hash_entries 0 sccs size 0 init   0.00ms df1 0.00ms sort 0.00ms dfs2 0.00ms setup-cb 0.00ms free-data 0.00ms links   0/0/0/0 dfs passes 0/0
[Mono] GC_MINOR: (Nursery full) pause 141.11ms, total 141.66ms, bridge 0.21ms promoted 0K major 688K los 0K
[Mono] Warning: Degraded allocation.  Consider increasing nursery-size if the warning persists.

I already increased the nursery-size to 8 but i get the error still. 我已经将托儿所的大小增加到8,但是仍然出现错误。

Try this: 尝试这个:

this.COMPANY_DATABASE_NAME = "company" + "v" + this.databaseVersion.ToString() + ".db"; this.COMPANY_DATABASE_NAME =“公司” +“ v” + this.databaseVersion.ToString()+“ .db”;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM