一、安装
sudoapt-get install sqlite sqlite3libsqlite3-dev
二、交互使用
$ sqlite3 stu.db sqlite version 3.11.0 2016-02-15 17:29:24Enter ".help" for usage hints.sqlite> CREATE table student(name,num);sqlite> insert into student values('wang',1002);sqlite> insert into student values('limin',1003);sqlite> select * from student;wang|1002limin|1003sqlite> .exit
三、c语言接口
代码MysqLite.c
#include <stdio.h> #include <sqlite3.h>#include <string.h> int printsqlResult(voID *para,int column,char **value,char **key) { int i; printf("column=%d\n",column); for(i = 0; i < column; i++) { printf("%s\t",*(value+i)); } printf("\n"); return sqlITE_OK; } int main(int argc,char *argv[]) { sqlite3 *pDb; int j,i,pos,row,col,ret; char acCmd[128]; char **ppRet; if(argc < 2) { printf("please input sql command!"); return -1; } strcpy(acCmd,argv[1]); ret = sqlite3_open("./stu.db",&pDb); if(ret != sqlITE_OK) { printf("open database fail!\n"); return -1; } ret = sqlite3_exec(pDb,acCmd,printsqlResult,NulL,NulL); { if(ret != sqlITE_OK) { printf("exec fail,ret %d\n",ret); return -1; } } pos = sqlite3_last_insert_rowID(pDb); printf("pos = %d\n",pos); sqlite3_get_table(pDb,"select * from student;",&ppRet,&row,&col,NulL); for(i = 0; i <= row; i++) { for(j = 0; j < col; j++) { printf("%s\t",*(ppRet+i*col+j)); } printf("\n"); } sqlite3_free_table(ppRet); sqlite3_close(pDb); return 0; }
Makefile
# Edit the following for your installationCC = gcc CFLAG = -g -Wall -O2 LFLAGS = `pkg-config --libs sqlite3`INCPATH = `pkg-config --cflags sqlite3`####### Implicit rules.c.o: $(CC) -c $(CFLAG) $(INCPATH) -o "$@" "$<"# file dependencIEs and rulesOBJs = MysqLite.oPRG = MysqLiteall: $(PRG) $(PRG): $(OBJs) $(CC) -o $(PRG) $(OBJs) $(LFLAGS)clean: rm -rf $(OBJs) $(PRG)
当手动编译安装在自己的路径时需要设置好环境变量,特别要设置PKG_CONfig_PATH,否则pkg-config会找不到sqlite3.pc(apt安装时该文件位置在/usr/lib/x86_64-linux-gnu/pkgconfig/sqlite3.pc)
运行
$ ./MysqLite "select * from student"column=2wang 1002 column=2limin 1003 pos = 0name num wang 1002 limin 1003
四、JDBC
下载驱动:
https://bitbucket.org/xerial/sqlite-jdbc/downloads/
wget https://bitbucket.org/xerial/sqlite-jdbc/downloads/sqlite-jdbc-3.18.0.jar
$ sqlite3 sample.db sqlite version 3.11.0 2016-02-15 17:29:24Enter ".help" for usage hints.sqlite> .exit
Java代码Sample.java
import java.sql.Connection;import java.sql.DriverManager;import java.sql.ResultSet;import java.sql.sqlException;import java.sql.Statement;public class Sample{ public static voID main(String[] args) throws ClassNotFoundException { // load the sqlite-JDBC driver using the current class loader Class.forname("org.sqlite.JDBC"); Connection connection = null; try { // create a database connection connection = DriverManager.getConnection("jdbc:sqlite:sample.db"); Statement statement = connection.createStatement(); statement.setqueryTimeout(30); // set timeout to 30 sec. statement.executeUpdate("drop table if exists person"); statement.executeUpdate("create table person (ID integer,name string)"); statement.executeUpdate("insert into person values(1,'leo')"); statement.executeUpdate("insert into person values(2,'yui')"); ResultSet rs = statement.executequery("select * from person"); while(rs.next()) { // read the result set System.out.println("name = " + rs.getString("name")); System.out.println("ID = " + rs.getInt("ID")); } } catch(sqlException e) { // if the error message is "out of memory",// it probably means no database file is found System.err.println(e.getMessage()); } finally { try { if(connection != null) connection.close(); } catch(sqlException e) { // connection close Failed. System.err.println(e); } } }}
如sample.db不在当前目录,可使用绝对路径
编译运行:
$ javac Sample.java$ java -classpath ".:sqlite-jdbc-3.18.0.jar" Sample name = leoID = 1name = yuiID = 2总结
以上是内存溢出为你收集整理的安装和使用sqlite数据库全部内容,希望文章能够帮你解决安装和使用sqlite数据库所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)