[實作] 在Raspberry Pi 3上建立SQLite

在Raspberry Pi 3上建立SQLite

本機環境:

Visual Studio 2017

DB Browser for SQLite

 

 

1. 安裝SQLite套件 SQLite for Universal Windows Platform

點選 工具 --> 擴充功能和更新 --> 搜尋輸入 SQLite for Universal Windows Platform --> 安裝

2. 建立一個Universal Windows 的專案,並加入參考

3. 安裝SQLitePCL套件

點選 工具 --> NuGet封裝管理員 --> 管理方案的NuGet套件 --> 輸入 SQLitePCL --> 安裝

4. 撰寫程式

在樹梅派上建立一個Iot5DB.sdf的SQLite資料庫檔案,Table Name 為 users,包含兩個欄位,id為Primary Key型別為integer,name型別為text,接著寫入三筆資料,分別為  IoT-1、IoT-2、IoT-3。

然後修改第二筆資料為"IoT-dd-HH:mm:ss"格式,時間為當前時間,接著刪除最後一筆資料。

範例程式如下:

        string _dbName = "IoT5DB.sdf";
        string _tableName = "users";

        public void WriteToDatabase()
        {
            try
            {
                //初始化
                string[] _names = { "IoT-1", "IoT-2", "IoT-3" };

                this.InitializeComponent();
                //簡易MVVM框架
                this.DataContext = this;
                //創建資料庫連接
                using (SQLiteConnection connection = CreateDbConnection())
                {
                    //創建表
                    CreateTable(connection);
                    foreach (string name in _names)
                    {
                        //插入數據
                        InsertRow(connection, name);
                    }
                    //更新第二條數據
                    UpdateRow(connection, string.Format("IoT-{0}", DateTime.Now.ToString("dd-HH:mm:ss")), _names[1]);
                    //刪除第一條數據
                    DeleteRow(connection, _names[0]);
                }
            }
            catch (Exception ex)
            {

                throw;
            }
        }


        private SQLiteConnection CreateDbConnection()
        {
            //創建連接
            SQLiteConnection connection = new SQLiteConnection(_dbName);
            if (null == connection)
            {
                throw new Exception("create db connection failed");
            }
            return connection;
        }

        private void CreateTable(SQLiteConnection connection)
        {
            //創建表
            string sql = string.Format("create table if not exists {0} (id integer primary key autoincrement,name text)", _tableName);
            using (ISQLiteStatement sqliteStatement = connection.Prepare(sql))
            {
                //執行語句
                sqliteStatement.Step();
            }
        }

        private void InsertRow(SQLiteConnection connection, string name)
        {
            //插入數據
            string sql = string.Format("insert into {0} (name) values (?)", _tableName);
            using (ISQLiteStatement sqliteStatement = connection.Prepare(sql))
            {
                //綁定參數
                sqliteStatement.Bind(1, name);
                //執行語句
                sqliteStatement.Step();
            }
        }

        private void UpdateRow(SQLiteConnection connection, string newName, string oldName)
        {
            string sql = string.Format("update {0} set name = ? where name = ?", _tableName);
            using (ISQLiteStatement sqliteStatement = connection.Prepare(sql))
            {
                //綁定參數
                sqliteStatement.Bind(1, newName);
                sqliteStatement.Bind(2, oldName);
                //執行語句
                sqliteStatement.Step();
            }
        }

        private void DeleteRow(SQLiteConnection connection, string name)
        {
            string sql = string.Format("delete from {0} where name = ?", _tableName);
            using (ISQLiteStatement sqliteStatement = connection.Prepare(sql))
            {
                //綁定參數
                sqliteStatement.Bind(1, name);
                //執行語句
                sqliteStatement.Step();
            }
        }

 

5. 開啟樹梅派的FTP功能,確認資料庫檔案是否存在

ftp://[IP]/Data/Users/DefaultAccount/AppData/Local/Packages/{Packge_Family_Name}/LocalState/

6. 安裝DB Browser for SQLite檢視資料是否正確

 

參考