【SQLite】简单的基本使用步骤
2017-07-02 16:11
405 查看
SQLite介绍
SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。
SQLite数据库官方主页:http://www.sqlite.org/index.html
第一步:dll的引用
C#下SQLite操作驱动dll下载:System.Data.SQLite,引用到项目,这个可以去官网下载也可以在vs的NuGet查找引用。
在需要使用的类添加System.Data.SQLite;
以上就是使用准备,非常简单。
第二步:使用sqlite
首先,创建一个空的数据库,代码
//数据库连接
SQLiteConnection m_dbConnection;
//创建一个空的数据库
void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
}
空的数据库已经创建完成,这个时候我们需要创捷链接字符。
//创建一个连接到指定数据库
void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
}
数据库已经链接成功,下面我们给它创建一个表,并且同时创建两个字段。
//在指定数据库中创建一个table
void createTable()
{
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
做到这里前提已经准备好了,你现在就已经拥有了一个sqlite数据库,并且数据库有一个叫做highscores 的表。下面我们往表里添加数据:
//插入一些数据
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查询语句,并显示结果
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())
MessageBox.Show("Name: " + reader["name"] + "\tScore: " + reader["score"]);
}
程序运行结果:
没有错messagebox弹出了我们插入的结果,通过结果我们可以知道我们的sqlite创建成功了。
此外还有一种方法也可以查看是否创建成功,就是进入debug文件找到一个与创建的数据库名字一样后缀为.sqlite的文件。
SQLite is a software library that implements a self-contained, serverless, zero-configuration, transactional SQL database engine.
SQLite是一个开源、免费的小型RDBMS(关系型数据库),能独立运行、无服务器、零配置、支持事物,用C实现,内存占用较小,支持绝大数的SQL92标准。
SQLite数据库官方主页:http://www.sqlite.org/index.html
第一步:dll的引用
C#下SQLite操作驱动dll下载:System.Data.SQLite,引用到项目,这个可以去官网下载也可以在vs的NuGet查找引用。
在需要使用的类添加System.Data.SQLite;
以上就是使用准备,非常简单。
第二步:使用sqlite
首先,创建一个空的数据库,代码
//数据库连接
SQLiteConnection m_dbConnection;
//创建一个空的数据库
void createNewDatabase()
{
SQLiteConnection.CreateFile("MyDatabase.sqlite");
}
空的数据库已经创建完成,这个时候我们需要创捷链接字符。
//创建一个连接到指定数据库
void connectToDatabase()
{
m_dbConnection = new SQLiteConnection("Data Source=MyDatabase.sqlite;Version=3;");
m_dbConnection.Open();
}
数据库已经链接成功,下面我们给它创建一个表,并且同时创建两个字段。
//在指定数据库中创建一个table
void createTable()
{
string sql = "create table highscores (name varchar(20), score int)";
SQLiteCommand command = new SQLiteCommand(sql, m_dbConnection);
command.ExecuteNonQuery();
}
做到这里前提已经准备好了,你现在就已经拥有了一个sqlite数据库,并且数据库有一个叫做highscores 的表。下面我们往表里添加数据:
//插入一些数据
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查询语句,并显示结果
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())
MessageBox.Show("Name: " + reader["name"] + "\tScore: " + reader["score"]);
}
程序运行结果:
没有错messagebox弹出了我们插入的结果,通过结果我们可以知道我们的sqlite创建成功了。
此外还有一种方法也可以查看是否创建成功,就是进入debug文件找到一个与创建的数据库名字一样后缀为.sqlite的文件。
相关文章推荐
- 使用V4L进行简单视频捕捉的基本步骤
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- iOS- 简单说说iOS移动客户端SQLite3的基本使用
- OpenGl中使用着色器的基本步骤及GLSL渲染简单示例
- 安卓jni 使用V4L进行简单视频捕捉的基本步骤
- 使用V4L进行简单视频捕捉的基本步骤
- [转]使用简单的 5 个步骤设置 Web 服务器集群
- SQLite的shell简单使用
- windbg基本简单步骤
- 使用HC(HttpClient)/UC(HttpURLConnection)进行网络访问的基本步骤:
- mytabis框架简单介绍及基本开发步骤
- Android之sqlite3命令行简单使用
- 使用Sqlite的一些简单问题[VC]
- 05-按钮的基本使用-开发步骤
- vue.js基本使用 利用webpack"热"运行vuejs+html初步和关键步骤 <二>
- git ,github的简单使用步骤
- Filestream 使用简单步骤
- 熟悉java语言的基本使用:简单存款取款机制java实现
- Android 自定义Camera基本使用步骤及关键注意点
- cocos2d-x中SQLite基本使用