2016-07-25 60 views
2

我已經在這個主題上工作了4個小時,但是我找不到任何解決方案。爲所有表格創建的Xamarin SQLite數據庫

我的問題其實是; 我有5桌,我想創建一個控制器來創建不同的表。

我目前的代碼在下面,但是這個代碼只創建一個表。

public interface ISQLite 
{ 
    SQLiteConnection GetConnection(); 
} 

-

public class TodoItem 
{ 
    public TodoItem() 
    { 
    } 

    [PrimaryKey, AutoIncrement] 
    public int ID { get; set; } 
    public string Name { get; set; } 
    public string Notes { get; set; } 
    public bool Done { get; set; } 
} 

-

public class TodoItemDatabase 
{ 
    static object locker = new object(); 

    SQLiteConnection database; 

    /// <summary> 
    /// Initializes a new instance of the <see cref="Tasky.DL.TaskDatabase"/> TaskDatabase. 
    /// if the database doesn't exist, it will create the database and all the tables. 
    /// </summary> 
    /// <param name='path'> 
    /// Path. 
    /// </param> 
    public TodoItemDatabase() 
    { 
     database = DependencyService.Get<ISQLite>().GetConnection(); 
     // create the tables 
     database.CreateTable<TodoItem>(); 
    } 

    public IEnumerable<TodoItem> GetItems() 
    { 
     lock (locker) { 
      return (from i in database.Table<TodoItem>() select i).ToList(); 
     } 
    } 

    public IEnumerable<TodoItem> GetItemsNotDone() 
    { 
     lock (locker) { 
      return database.Query<TodoItem>("SELECT * FROM [TodoItem] WHERE [Done] = 0"); 
     } 
    } 

    public TodoItem GetItem (int id) 
    { 
     lock (locker) { 
      return database.Table<TodoItem>().FirstOrDefault(x => x.ID == id); 
     } 
    } 

    public int SaveItem (TodoItem item) 
    { 
     lock (locker) { 
      if (item.ID != 0) { 
       database.Update(item); 
       return item.ID; 
      } else { 
       return database.Insert(item); 
      } 
     } 
    } 

    public int DeleteItem(int id) 
    { 
     lock (locker) { 
      return database.Delete<TodoItem>(id); 
     } 
    } 
} 

-

public class SQLite_Android : ISQLite 
{ 
    public SQLite_Android() 
    { 
    } 

    #region ISQLite implementation 
    public SQLite.SQLiteConnection GetConnection() 
    { 
     var sqliteFilename = "TodoSQLite.db3"; 
     string documentsPath = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal); // Documents folder 
     var path = Path.Combine(documentsPath, sqliteFilename); 

     // This is where we copy in the prepopulated database 
     Console.WriteLine(path); 
     if (!File.Exists(path)) 
     { 
      var s = Forms.Context.Resources.OpenRawResource(Resource.Raw.TodoSQLite); // RESOURCE NAME ### 

      // create a write stream 
      FileStream writeStream = new FileStream(path, FileMode.OpenOrCreate, FileAccess.Write); 
      // write to the stream 
      ReadWriteStream(s, writeStream); 
     } 

     var conn = new SQLite.SQLiteConnection(path); 

     // Return the database connection 
     return conn; 
    } 
    #endregion 

    /// <summary> 
    /// helper method to get the database out of /raw/ and into the user filesystem 
    /// </summary> 
    void ReadWriteStream(Stream readStream, Stream writeStream) 
    { 
     int Length = 256; 
     Byte[] buffer = new Byte[Length]; 
     int bytesRead = readStream.Read(buffer, 0, Length); 
     // write the required bytes 
     while (bytesRead > 0) 
     { 
      writeStream.Write(buffer, 0, bytesRead); 
      bytesRead = readStream.Read(buffer, 0, Length); 
     } 
     readStream.Close(); 
     writeStream.Close(); 
    } 
} 

---我怎樣才能創建一個控制器多表?

回答

1

看起來你使用的是Sqlite.net-pcl,對不對?

不支持來自同一模型的多個表格(僅用於簡單情況)。

您可以創建多個模型(可能通過繼承),然後爲每個模型調用CreatTable<T>

+0

是的我正在使用Sqlite.net-pcl。好的,非常感謝。我會用intercafe。對? – Aybuke

1

我解決了問題。也許這個解決方案有助於一個人。

我有兩個DbHepler類和兩個模型類在DB上創建兩個表。

基本連接碼相同;

public interface ISQLite 
{ 
    SQLiteConnection GetConnection(); 
} 

這是App.cs文件;

public class App : Application { 
    public App() 
    { 
     authenticationDB = new AuthenticationDbHelper(Database); 
     settingsDbHelper = new SettingsDbHelper(Database); 
     MainPage = new Views.MainMenuPage(); 
    } 
public static CreateDB Database 
    { 
     get 
     { 
      if (database == null) 
      { 
       database = new CreateDB(); 
      } 
      return database; 
     } 
    } 
} 

的CREATEDB類是必需的所有表

public class CreateDB 
{ 
    public SQLiteConnection database; 
    public object locker = new object(); 
    public CreateDB() 
    { 
     database = DependencyService.Get<ISQLite>().GetConnection(); 
    } 

} 

這個接口是必要的創建表的動作製作一個分貝。由於實現這個類,我們可以使用theese方法的所有表。(T爲表類)(要了解一下AuthenticationDBHelper類)

public interface SQLiteBase<T> 
{ 
    IEnumerable<T> GetItems(); 
    T GetItem(long id); 
    long SaveItem(T item); 
    void UpdateItem(T item); 
    int DeleteItem(int id); 
    int Clear(); 
    int getCount(); 
} 

這DbHelper類將用於刪除,插入,明確....項目。

public class AuthenticationDbHelper : SQLiteBase<AuthenticationDbTable> 
{ 
    SQLiteConnection database; 
    object locker; 
    public AuthenticationDbHelper(CreateDB db) 
    { 
     database = db.database; 
     locker = db.locker; 
     database.CreateTable<AuthenticationDbTable>(); 
    } 

    public int Clear() 
    { 
     lock(locker) 
     { 
      return database.DeleteAll<AuthenticationDbTable>(); 
     } 
    } 

    public int DeleteItem(int id) 
    { 
     lock (locker) 
     { 
      return database.Delete<AuthenticationDbTable>(id); 
     } 
    } 

    public AuthenticationDbTable GetItem(long id) 
    { 
     lock (locker) 
     { 
      return database.Table<AuthenticationDbTable>().FirstOrDefault(x => x.UserId == id); 
     } 
    } 

    public IEnumerable<AuthenticationDbTable> GetItems() 
    { 
     lock (locker) 
     { 
      return (from i in database.Table<AuthenticationDbTable>() select i).ToList(); 
     } 
    } 

    public long SaveItem(AuthenticationDbTable item) 
    { 
     lock (locker) 
     { 
      return database.Insert(item); 
     } 
    } 

    public void UpdateItem(AuthenticationDbTable item) 
    { 
     lock(locker) 
     { 
      database.Update(item); 
     } 
    } 

    public int getCount() 
    { 
     return GetItems().Count(); 
    } 

} 

我知道這是很困惑,但這是最後一次。我們將創建身份驗證模型。

public class AuthenticationDbTable 
{ 

    public AuthenticationDbTable(long userId, string sessionId, string username, string clientuuid) 
    { 
     this.userId = userId; 
     this.sessionId = sessionId; 
     this.username = username; 
     this.clientuuid = clientuuid; 
    } 


    private long userId; 
    private string sessionId; 
    private string username; 
    private string clientuuid; 

    [PrimaryKey] 
    public long UserId 
    { 
     get { return userId; } 
     set { userId = value; } 
    } 

    public string SessionId 
    { 
     get { return sessionId; } 
     set { sessionId = value; } 
    } 

    public string Username 
    { 
     get { return username; } 
     set { username = value; } 
    } 

    public string Clientuuid 
    { 
     get { return clientuuid; } 
     set { clientuuid = value; } 
    } 
} 

使用

AuthenticationDbTable authentication = new AuthenticationDbTable(authenticateduser.User.UserId, r.Retval.SessionStatus.SessionId, authenticateduser.User.Name, authenticateduser.Clientuuid); 
App.authenticationDB.SaveItem(authentiaction); 

注意

爲了創建第二個表,你可以使用同樣的方法。你應該創建第二個DbHelper和模型類。假設您將創建一個設置表。您應該創建SettingsDbHelper和SettingsDbTable類。通過相同的方式。

謝謝:)