自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳解Qt 連接SQLite操作

移動開發(fā)
本文介紹的是Qt 連接SQLite操作,SQLite如今也是一個輕型的數(shù)據(jù)庫了。我們先來看內(nèi)容的具體操作。

Qt 連接SQLite操作是本文要介紹的內(nèi)容,本文主要是介紹往數(shù)據(jù)庫中添加數(shù)據(jù)的插入操作,首先下載SQLite數(shù)據(jù)庫

首先到SQLite官方網(wǎng)站下載:

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

得到sqlite3.exe。即可.就可以操作數(shù)據(jù)庫了。

運行cmd到該指定的目錄下,使用如下命令:如

F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17>sqlite3.exe test 

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

就可以創(chuàng)建一個名為test的數(shù)據(jù)庫了.你可以使用.help命令查看各命令.

.databases   可以得到所有的數(shù)據(jù)庫。

  可以使用如下命令得到一張表,并插入數(shù)據(jù)。***.quit退出.
 
F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17> create table student(id varchar(10),name varchar(20),age smallint);  

F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17> select * from student;  

F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17> insert into student values('1001' , 'lovesizhao' ,26);  

F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17> select * from student;  

  1. 1001|lovesizhao|26    

F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17> drop table student;  

F:\軟件\學(xué)習(xí)軟件\數(shù)據(jù)庫\SQlite\sqlite-3_6_17> .quit 

而drop table student;   可以刪除該表格.其實大部操作都屬于SQL的相同沒什么改變.

也可以將該數(shù)據(jù)庫備份至output.sql,也稱為重定向

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

***可以將數(shù)據(jù)庫保存為test.db3即可。

下面講解如何通過Qt來訪問剛才建立的數(shù)據(jù)庫:

  1. QSqlDatabase dbconn = QSqlDatabase::addDatabase("QSQLITE", "testSQLite");     
  2.  
  3.    dbconn.setDatabaseName("test.db3");  //當(dāng)前目錄下的test.db3數(shù)據(jù)庫文件     
  4.      
  5.    //SQLite數(shù)據(jù)庫文件可用SQLite的命令行工具(c:\sqlite3.exe 數(shù)據(jù)庫名)或用SQLite GUI工具創(chuàng)建,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);   

也可以直接訪問內(nèi)存得到:如

  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')"); 

 以下的操作只是往數(shù)據(jù)庫中添加數(shù)據(jù)的插入操作.如果想進(jìn)一步學(xué)習(xí),請查找相當(dāng)資料。

小結(jié):關(guān)于詳解Qt 連接SQLite操作的內(nèi)容介紹完了,希望本文對你有所幫助!更多關(guān)于數(shù)據(jù)庫的內(nèi)容請參考編輯推薦。

責(zé)任編輯:zhaolei 來源: 互聯(lián)網(wǎng)
相關(guān)推薦

2011-07-26 18:11:56

iPhone Sqlite 數(shù)據(jù)庫

2011-07-05 14:46:34

2011-07-04 17:26:00

Qt SQLite

2011-07-04 17:45:45

Qt Sqlite 數(shù)據(jù)庫

2011-07-01 14:06:57

Qt sqlite

2011-07-05 10:16:16

Qt 數(shù)據(jù)庫 SQLite

2011-07-05 17:54:43

QT Sqlite ARM

2011-06-24 10:54:34

Qt Mysql

2011-07-05 10:44:51

Qt Sqlite 靜態(tài)編譯

2011-07-05 10:22:44

Qt Sqlite

2011-07-05 09:54:04

2011-08-30 14:25:06

QT數(shù)據(jù)庫

2011-07-05 09:44:31

QT Mysql 亂碼

2011-07-05 10:03:00

Qt MYSQL 數(shù)據(jù)庫

2011-07-05 18:11:13

Qt 數(shù)據(jù)庫

2011-06-27 12:56:28

2011-07-05 17:38:52

QT Sqlite

2011-07-05 09:35:52

Ubuntu Qt Mysql

2011-06-23 11:16:39

Qt Excel

2011-06-24 10:05:51

QT 對象 父對象
點贊
收藏

51CTO技術(shù)棧公眾號