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

标签 c# sqlite tizen

我今天尝试了所有方法,特别是 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!");
        }
    }

SQLite数据库

public class SQLiteDatabase

{ 私有(private)只读字符串_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();
    }
}

关于c# - 如何在 Tizen .net (c#) 中使用 sqlite?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55464505/

相关文章:

c# - 动态添加的 ASP.NET 控件在后面的代码中不可见

android - 检查数据是否存在于 SQLite Android

android - 设备 ID 混淆和 SIGNATURE_INVALID

c# - 每当数据库更新时更新 C# 客户端

c# - 如果覆盖 0-9 则减少整数

caching - 是否可以针对不同的数据库调整不同的内存缓存大小?

tizen - Galaxy watch 无法连接到 Tizen Studio

c# - 处理 SQL 连接

php - 如何使用 PHP 将 mysql 转换为 sqlite3