SQLite c#

SQLite c#

BY 01 Apr 2019 Tizen .NET

Hi! I’m trying to port my wear os app to tizen (due to switching from ticwatch pro to galaxy watch), but i have some problems. I have a db in my shared/res directory in visual studio project, and i’m trying to open it with sqlite (throught methods of System.Data.SQLite), without success, because i obtain a segmentation error when i create sqlite connection to db…is shared/res maybe read only directory, as in android? If yes, how can i solve the problem?

Here is the code:.

MainApps:

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

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

SQLiteDatabase.cs:

public class SQLiteDatabase
    {
        private readonly string _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();
            }
        }
    }

 

Written by