C#与SQLite数据库
1.添加引用 System.Data.SQLite.dll
2.using System.Data.SQLite;
3.原理步骤:
string path = "c:\\mydb.db";
if (!File.Exists(path)) //1.如果数据库不存在,就创建数据库
{
SQLiteConnection.CreateFile(path);
}
SQLiteConnection conn = new SQLiteConnection("Data Source="+path); //2.连接数据库
conn.Open(); //3.打开数据库
SQLiteCommand comm = conn.CreateCommand(); //4.创建命令
comm.CommandText = "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='tablename'"; //5.给命令传sql语句,搜索tablename表是否存在
SQLiteDataReader reader = comm.ExecuteReader(); //6.执行命令
reader.Read(); //7.获取结果的"指针"
int num = reader.GetInt32(); //8.第一行数据的第0个值
reader.Close(); //关闭reader,如果不关闭,下面使用comm命令执行sql语句会报错
conn.Close(); //关闭连接
将txt中的数据写入到sqlite数据库中的函数。//参数file是txt文件的路径名
static void TxtToSQLite(string file)
{
string path = "c:\\mydb.db";
string sqlData = "(";
if (!File.Exists(path))
{
SQLiteConnection.CreateFile(path);
}
SQLiteConnection conn = new SQLiteConnection("Data Source=" + path);
conn.Open();
SQLiteCommand comm = conn.CreateCommand(); StreamReader sReader = new StreamReader(file);
string strLine = sReader.ReadLine(); //读取文件的第一行
string tableName = "year" + strLine.Substring(, );
//如果数据库中没有这个表就创建一个
comm.CommandText = "CREATE TABLE IF NOT EXISTS " + tableName + "(ID integer, altitude double, longitude double, p4 integer, p5 integer, p6 integer, p7 integer, p8 integer, p9 integer, p10 integer, p11 integer, p12 integer, p13 integer)";
comm.ExecuteNonQuery();
while (strLine != null)
{
while (strLine.IndexOf(" ")!=-)
{
strLine = strLine.Replace(" ", " ");
}
strLine = strLine.Replace(" ", ",");
sqlData = sqlData+strLine+"),(";
strLine = sReader.ReadLine();
}
sqlData = sqlData.Remove(sqlData.Length - , );
comm.CommandText = "INSERT INTO " + tableName + " VALUES " + sqlData ;
try
{
comm.ExecuteNonQuery();
}
catch (System.Exception ex)
{
Debug.WriteLine(ex.Message);
}
sReader.Close();
conn.Close();
}
txt数据格式:
说明:sqlite_master 、 SQLITE_TEMP_MASTER(存储临时表)
获得所有表的列表:
SELECT name FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master) WHERE type=’table’ ORDER BY name
下载:System.Data.SQLite.dll http://files.cnblogs.com/files/lwngreat/System.Data.SQLite.rar
C#与SQLite数据库的更多相关文章
- Android之SQLite数据库篇
一.SQLite简介 Google为Andriod的较大的数据处理提供了SQLite,他在数据存储.管理.维护等各方面都相当出色,功能也非常的强大. 二.SQLite的特点 1.轻量级使用 SQLit ...
- Qt5 开发 iOS 应用之访问 SQLite 数据库
开发环境: macOS 10.12.1 Xcode 8.1 Qt 5.8 iPhone 6S+iOS 10.1.1 源代码: 我在 Qt 程序里指定了数据库的名称来创建数据库,在 Win10.An ...
- 【Win 10 应用开发】Sqlite 数据库的简单用法
如果老周没记错的话,园子里曾经有朋友写过如何在 UWP 项目中使用 Sqlite数据库的文章.目前我们都是使用第三方封装的库,将来,SDK会加入对 Sqlite 的支持. 尽管目前 UWP-RT 库中 ...
- Android之SQLite数据库使用
转载整理于:http://my.csdn.net/lmj623565791 我刚开始接触Android的时候甚至都不敢相信,Android系统竟然是内置了数据库的!好吧,是我太孤陋寡闻了.由于我之前是 ...
- 让PDF.NET支持最新的SQLite数据库
最近项目中用到了SQLite,之前项目中用的是PDF.NET+MySQL的组合,已经写了不少代码,如果能把写好的代码直接用在SQLite上就好了,PDF.NET支持大部分主流的数据库,这个当然可以,只 ...
- iOS sqlite数据库图像化查看
问题描述:在xocde上用sqlite数据库的时候,因为没有图形化界面,有些时候很难看出自己设计的数据库是否有问题,比如我刚上手sqlite数据库设计id为自增长时,很自然的用了identify(1, ...
- Android中SQLite数据库小计
2016-03-16 Android数据库支持 本文节选并翻译<Enterprise Android - Programing Android Database Applications for ...
- Android开发-之SQLite数据库
之前我们讲了如何将数据存储在文件中,那么除了这种方式呢,就是我们常见的大家都知道的将数据存储在数据库当中了. 将数据存储在数据库中的优势: 1)存储在数据库中的数据更加方便操作,比如增.删.改.查等 ...
- Java操作Sqlite数据库-jdbc连接
Java操作Sqlite数据库步骤: 1. 导入Sqlite jdbc 本文使用sqlite-jdbc-3.7.2.jar,下载地址 http://pan.baidu.com/s/1kVHAGdD 2 ...
- Android开发学习——SQLite数据库与单元测试
SQLite数据库 轻量级关系型数据库 创建数据库需要使用的api:SQLiteOpenHelper public class Myopenhelper extends SQLiteOpenHelp ...
随机推荐
- Python3.5入门学习记录-函数
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...
- uva 10935 throwing cards away <queue>
Given is an ordered deck of n cards numbered 1 to n with card 1 at the top and card n ...
- The Letter Carrier's Rounds(摘)
Description For an electronic mail application you are to describe the SMTP-based communication that ...
- Mac OS X和iOS上基本数据类型的字节数
在Mac和IPhone上分别运行下面的程序: NSLog(@"char size = %lu", sizeof(char)); NSLog(@"short int siz ...
- 【面向代码】学习 Deep Learning(三)Convolution Neural Network(CNN)
========================================================================================== 最近一直在看Dee ...
- Wordpress更换主题之后出错
今天吃完午饭,休息休息,最近搞了一下google adsense,不过最终的审核没通过,我想会不会是界面不好看呢,饭后就在电脑旁,更换了几个wordpress主题,我的博客使用wordpress搭建的 ...
- python2 ----函数字典的使用
问题背景: 最近在用python2为sublime2写一个插件,其中有一个命令功能,就是输入不同的命令调用不同的函数,但是python不支持switch,只用ifelse的话感觉特别的low而且明显不 ...
- Lost Cows(BIT poj2182)
Lost Cows Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10609 Accepted: 6797 Descri ...
- jQuery.data的是jQuery的数据缓存系统
jQuery.Data源码 jQuery.data的是jQuery的数据缓存系统 jQuery.data的是jQuery的数据缓存系统.它的主要作用就是为普通对象或者DOM元素添加数据. 1 内部存储 ...
- Eclipse 乱码解决方案(UTF8 -- GBK)
UTF8 --> GBK; GBK --> UTF8 eclipse的中文乱码问题,一般不外乎是由操作系统平台编码的不一致导致,如Linux中默认的中文字体编码问UTF8, 而Wind ...