详解Qt 连接SQLite操作

移动开发
本文介绍的是Qt 连接SQLite操作,SQLite如今也是一个轻型的数据库了。我们先来看内容的具体操作。

Qt 连接SQLite操作是本文要介绍的内容,本文主要是介绍往数据库中添加数据的插入操作,首先下载SQLite数据库

首先到SQLite官方网站下载:

http://www.sqlite.org/download.html

得到sqlite3.exe。即可.就可以操作数据库了。

运行cmd到该指定的目录下,使用如下命令:如

F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17>sqlite3.exe test 

并有以下提示:Enter SQL statements terminated with a ";"  

就可以创建一个名为test的数据库了.你可以使用.help命令查看各命令.

.databases   可以得到所有的数据库。

  可以使用如下命令得到一张表,并插入数据。***.quit退出.
 
F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17> create table student(id varchar(10),name varchar(20),age smallint);  

F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17> select * from student;  

F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17> insert into student values('1001' , 'lovesizhao' ,26);  

F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17> select * from student;  

  1. 1001|lovesizhao|26    

F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17> drop table student;  

F:\软件\学习软件\数据库\SQlite\sqlite-3_6_17> .quit 

而drop table student;   可以删除该表格.其实大部操作都属于SQL的相同没什么改变.

也可以将该数据库备份至output.sql,也称为重定向

  1. sqlite3.exe test.db3 >output.sql 

***可以将数据库保存为test.db3即可。

下面讲解如何通过Qt来访问刚才建立的数据库:

  1. QSqlDatabase dbconn = QSqlDatabase::addDatabase("QSQLITE", "testSQLite");     
  2.  
  3.    dbconn.setDatabaseName("test.db3");  //当前目录下的test.db3数据库文件     
  4.      
  5.    //SQLite数据库文件可用SQLite的命令行工具(c:\sqlite3.exe 数据库名)或用SQLite GUI工具创建,SQLiteSpy     
  6.  
  7.    if(!dbconn.open())     
  8.    {     
  9.             
  10.        return;     
  11.    }     
  12.    
  13.    QTableView *view;     
  14.    QSqlTableModel *model;     
  15.    view = new QTableView();     
  16.    model = new QSqlTableModel(this,dbconn);     
  17.    model->setTable("test");     
  18.    model->select();     
  19.    view->setModel(model);   

也可以直接访问内存得到:如

  1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");  
  2.     db.setDatabaseName(":memory:");  
  3.     if (!db.open()) {  
  4.         QMessageBox::critical(0, qApp->tr("Cannot open database"),  
  5.             qApp->tr("Unable to establish a database connection.\n"  
  6.                      "This example needs SQLite support. Please read "  
  7.                      "the Qt SQL driver documentation for information how "  
  8.                      "to build it.\n\n"  
  9.                      "Click Cancel to exit."), QMessageBox::Cancel);  
  10.         return false;  
  11.     }  
  12.  
  13.     QSqlQuery query;  
  14.     query.exec("create table person (id int primary key, "  
  15.                "firstname varchar(20), lastname varchar(20))");  
  16.     query.exec("insert into person values(101, 'Danny', 'Young')");  
  17.     query.exec("insert into person values(102, 'Christine', 'Holand')");  
  18.  
  19.     query.exec("create table images (locationid int, file varchar(20))");  
  20.     query.exec("insert into images values(0, 'images/oslo.png')");  
  21.     query.exec("insert into images values(1, 'images/brisbane.png')"); 

 以下的操作只是往数据库中添加数据的插入操作.如果想进一步学习,请查找相当资料。

小结:关于详解Qt 连接SQLite操作的内容介绍完了,希望本文对你有所帮助!更多关于数据库的内容请参考编辑推荐。

责任编辑:zhaolei 来源: 互联网
相关推荐

2011-07-26 18:11:56

iPhone Sqlite 数据库

2011-07-05 14:46:34

2011-07-05 10:16:16

Qt 数据库 SQLite

2011-07-05 17:54:43

QT Sqlite ARM

2011-07-01 14:06:57

Qt sqlite

2011-07-04 17:45:45

Qt Sqlite 数据库

2011-07-04 17:26:00

Qt SQLite

2011-06-24 10:54:34

Qt Mysql

2011-07-05 10:44:51

Qt Sqlite 静态编译

2011-07-05 10:22:44

Qt Sqlite

2011-07-05 09:54:04

2011-08-30 14:25:06

QT数据库

2011-07-05 09:44:31

QT Mysql 乱码

2011-07-05 10:03:00

Qt MYSQL 数据库

2011-06-27 12:56:28

2011-07-05 18:11:13

Qt 数据库

2011-07-05 17:38:52

QT Sqlite

2011-08-30 14:15:34

QTSQLite数据库

2011-07-05 09:35:52

Ubuntu Qt Mysql

2013-01-06 12:23:59

Android开发SQLite数据库
点赞
收藏

51CTO技术栈公众号