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

时间:2015-04-07 14:19:01

标签: c# sqlite xamarin xamarin.android

我的应用程序中有两个数据库,我是在SQLiteOpenHelper类的帮助下创建的。我决定取消SQLiteOpenHelper,只使用SQLite组件创建数据库。我有一个databaseHandler类,我创建了dbs和tabless.问题是我似乎只能创建一个数据库。如果我添加第二个数据库名称要创建,我启动时会出现黑屏然后崩溃。在我看来,这两个dbs可以& #39; t在同一地点共存。我不知道如何解决这个问题。

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);


        }
   }

}

当我取消注释我为公司数据库分配名称的行时发生崩溃:

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

并给我以下输出:

[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.

我已经将幼儿园大小增加到8但我仍然得到错误。

1 个答案:

答案 0 :(得分:0)

试试这个:

this.COMPANY_DATABASE_NAME =&#34; company&#34; +&#34; v&#34; + this.databaseVersion.ToString()+&#34; .db&#34;;