我通过添加 SQLite 组件开发了我的第一个 ToDo 应用程序,但我无法在屏幕上查看已保存的任务。我不确定我是否正确创建了数据库文件。我想我在这里搞砸了:
public class TaskViewModel
{
SQLiteConnection conn ;
public TaskViewModel ()
{
string folder = Environment.GetFolderPath (Environment.SpecialFolder.Personal);
conn = new SQLiteConnection (System.IO.Path.Combine (folder, "tasks.db"));
conn.CreateTable<Task> ();
}
}
最佳答案
您的连接和文件创建似乎没问题,但您可能在更新 View 时遇到问题。查看如何将数据绑定(bind)到 View 以及用于更新它的事件。
请记住在使用 SQL 连接时始终使用 Open
和 Close
。
看看我用来启动数据绑定(bind)过程的类:
class Repository
{
public const string db_file = "tasks.db3";
public static string db_path = Path.Combine (System.Environment.GetFolderPath (System.Environment.SpecialFolder.Personal), db_file);
public static SqliteConnection GetConnection ()
{
bool exists = File.Exists (db_path);
if (!exists)
SqliteConnection.CreateFile (db_path);
var conn = new SqliteConnection ("Data Source=" + db_path);
if (!exists)
CreateDatabase (conn);
return conn;
}
public static Person Task { get; set; }
private static void CreateDatabase (SqliteConnection conn)
{
var sql = @"CREATE TABLE PERSON (
Id INTEGER PRIMARY KEY AUTOINCREMENT,
Name VARCHAR(250),
Description VARCHAR(250),
Enabled BOOLEAN,
Timestamp DATETIME
);";
conn.Open ();
using (var cmd = conn.CreateCommand ()) {
cmd.CommandText = sql;
cmd.ExecuteNonQuery ();
}
conn.Close ();
}
}
关于c# - Xamarin 使用SQLite 组件。无法在屏幕上查看已保存的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21223043/