c# andriod开发 sqlite操作(引用Mono.Data.Sqlite.Portable包)

c# andriod开发 sqlite操作(引用Mono.Data.Sqlite.Portable包)
引用Mono.Data.Sqlite.Portable包

开始引用System.Data.SQLite时一直报错,缺少System.Data.SQLite.dll,换了几个版本没解决问题,后改为Mono.Data.Sqlite.Portable包,操作功能基本相同。
c# andriod开发 sqlite操作(引用Mono.Data.Sqlite.Portable包)_第1张图片

using Mono.Data.Sqlite;
using System;

using System.IO;
using Environment = System.Environment;

namespace SqliteTest
{
    public class testSqlite
    {
        public SqliteConnection m_dbConnection;

        public testSqlite()
        {
            createNewDatabase();
            connectToDatabase();
            createTable();
            fillTable();
            printHighscores();
        }

        //创建一个空的数据库
        public string createNewDatabase()
        {
            // SQLiteConnection.CreateFile("MyDatabase.sqlite");
            string dbPath = Path.Combine(
            Environment.GetFolderPath(Environment.SpecialFolder.Personal),
           "database.db3");
            SqliteConnection.CreateFile(dbPath);
            return dbPath;
        }

        //创建一个连接到指定数据库
        public void connectToDatabase()
        {
            m_dbConnection = new SqliteConnection("Data Source=/data/user/0/SqliteTest.SqliteTest/files/database.db3;Version=3;");
            m_dbConnection.Open();
        }

        //在指定数据库中创建一个table
        public void createTable()
        {
            string sql = "create table highscores (name varchar(20), score int)";
            SqliteCommand command = new SqliteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //插入一些数据
        public void fillTable()
        {
            string sql = "insert into highscores (name, score) values ('Me', 3000)";
            SqliteCommand command = new SqliteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into highscores (name, score) values ('Myself', 6000)";
            command = new SqliteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();

            sql = "insert into highscores (name, score) values ('And I', 9001)";
            command = new SqliteCommand(sql, m_dbConnection);
            command.ExecuteNonQuery();
        }

        //使用sql查询语句,并显示结果
        public void printHighscores()
        {
            string sql = "select * from highscores order by score desc";
            SqliteCommand command = new SqliteCommand(sql, m_dbConnection);
            SqliteDataReader reader = command.ExecuteReader();
            while (reader.Read())
                Console.WriteLine("Name: " + reader["name"] + "\tScore: " + reader["score"]);
            Console.ReadLine();
        }
    }
}

你可能感兴趣的:(c#/android,sqlite,c#)