簡體   English   中英

如何在 Tizen .net (c#) 中使用 sqlite?

[英]How can i use sqlite in Tizen .net (c#)?

我今天嘗試了所有方法,尤其是 System.data.SQLite,它在我嘗試創建新的 SQLiteConnection 時會生成 SegmentationFault。 我在共享/資源中有一個本地數據庫,我想詢問它以檢索一些數據。 有人可以幫助我嗎?

App.xaml.cs

protected override void OnStart()
    {
        // Handle when your app starts
        String db_name = "CTM_BUS.db";
        String old_db_path = "/opt/usr/apps/com.feduss.example.BusFinder.Tizen.Wearable/shared/res/";


        if (File.Exists(old_db_path + db_name))
        {

            SQLiteDatabase db = new SQLiteDatabase(new_db_path);
            SQLiteCommand selectBusLines = new SQLiteCommand
            ("SELECT * from Routes");
            DataTable res = db.GetDataTable(selectBusLines);
        }
        else
        {
            Console.WriteLine("File does not exist!");
        }
    }

SQLiteDatabase

public class SQLiteDatabase

{ 私有只讀字符串 _dbConnection;

    public SQLiteDatabase(string dataSource)
    {
        _dbConnection = string.Format("Data Source={0}", dataSource);
    }

    public DataTable GetDataTable(SQLiteCommand command)
    {
        if (command == null) throw new ArgumentNullException("command");

        using (SQLiteConnection connection = new SQLiteConnection(_dbConnection))
        {
            connection.Open();
            command.Connection = connection;

            using (SQLiteDataReader reader = command.ExecuteReader())
            {
                DataTable result = new DataTable();
                result.Load(reader);
                return result;
            }
        }
    }

    public SQLiteCommand GetCommand(string sql)
    {
        if (string.IsNullOrEmpty(sql))
            throw new ArgumentNullException("sql");

        return new SQLiteCommand
        {
            CommandText = sql,
            CommandType = CommandType.Text
        };
    }

    public int ExecuteNonQuery(SQLiteCommand command)
    {
        if (command == null) throw new ArgumentNullException("command");

        using (SQLiteConnection connection = new SQLiteConnection(_dbConnection))
        {
            connection.Open();
            command.Connection = connection;

            return command.ExecuteNonQuery();
        }
    }
}

使用以下 nuget 包:

  • sqlite-net-base
  • SQLitePCLRaw.core
  • SQLitePCLRaw.provider.sqlite3.netstandard11

然后:

定義表模式,例如:

public class TableSchema
{
    [AutoIncrement, PrimaryKey]
    public int Id { get; set; }

    public int Column1 { get; set; } = 0;
    public int Column2 { get; set; } = 0;
}

BaseRepository 類 - 它連接到數據庫並返回處理程序:

public abstract class BaseRepository
{
    private readonly string _databasePath;

    protected BaseRepository(string databasePath)
    {
        _databasePath = databasePath;
    }

    protected SQLite.SQLiteConnection ConnectDatabase()
    {
        SQLitePCL.raw.SetProvider(new SQLitePCL.SQLite3Provider_sqlite3());
        SQLitePCL.raw.FreezeProvider();

        return new SQLite.SQLiteConnection(_databasePath);
    }
}

存儲庫類(包含常用查詢的包裝器):

public class Repository : BaseRepository
{
    public Repository(string databasePath) : base(databasePath)
    {
    }

    public List<TableSchema> GetAll()
    {
        using (SQLite.SQLiteConnection connection = ConnectDatabase())
        {
            return connection.Table<TableSchema>().ToList();
        }
    }

    public List<TableSchema> Query(string query)
    {
        using (SQLite.SQLiteConnection sqlConnection = ConnectDatabase())
        {
            try
            {
                SQLite.SQLiteCommand queryCommand = sqlConnection.CreateCommand(query);
                List<TableSchema> results = queryCommand.ExecuteQuery<TableSchema>();

                return results;
            }
            catch (System.Exception e)
            {
                global::Tizen.Log.Error("TEST_TAG", e.Message);

                return new List<TableSchema>();
            }
        }
    }
}

class Sample
{
    Repository _mapRepository;

    public Sample()
    {
        _mapRepository = new Repository(App.Current.DirectoryInfo.Data + "db.sqlite3");

         List<Table> allRecords = _mapRepository.GetAll();
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM