當前位置:首頁 » 操作系統 » sqlite資料庫Qt

sqlite資料庫Qt

發布時間: 2022-04-22 22:33:35

Ⅰ 怎樣在Qt下連接讀寫sqlite資料庫

採用將項目中的sqlite文件拷到android內存卡上的固定位置,先判斷位置上是否存在sqlite文件,不存在則復制文件。 sqlite可放在res的raw目錄下,亦可放在assets下,放在assets下,可新建其他層次目錄,本例選擇assets
1.首先,添加sdcard許可權在AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在activity中有如下代碼

2.Java代碼

button2.setOnClickListener(new Button.OnClickListener() {

@Override
publicvoid onClick(View arg0) {
try{
String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
+ "/testdb"; //將要存放於的文件夾
String DATABASE_FILENAME = "testDatabase.db"; //文件名
String databaseFilename = DATABASE_PATH + "/" + DATABASE_FILENAME;
File dir = new File(DATABASE_PATH);
// 如果/sdcard/testdb目錄中存在,創建這個目錄
if (!dir.exists())
dir.mkdir();
// 如果在/sdcard/testdb目錄中不存在
// test.db文件,則從asset\db目錄中復制這個文件到
// SD卡的目錄(/sdcard/testdb)
if (!(new File(databaseFilename)).exists()) {
// 獲得封裝testDatabase.db文件的InputStream對象
AssetManager asset=getAssets();
InputStream is=asset.open("db/testDatabase.db");
FileOutputStream fos = new FileOutputStream(databaseFilename);
byte[] buffer = newbyte[8192];
int count = 0;
// 開始復制testDatabase.db文件
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
asset.close();
}
SQLiteDatabase mSQLiteDatabase=openOrCreateDatabase(databaseFilename, Activity.MODE_PRIVATE, null);//有則打開,沒有創建
Cursor cur=mSQLiteDatabase.rawQuery("select * from table1", null);
if(cur!=null){
if(cur.moveToFirst()){
do{
int idColumnIndex=cur.getColumnIndex("id");
int numColumnIndex=cur.getColumnIndex("num");
int dataColumnIndex=cur.getColumnIndex("data");
int id=cur.getInt(idColumnIndex);
int num=cur.getInt(numColumnIndex);
String data=cur.getString(dataColumnIndex);
System.out.println("id:"+id+";num:"+num+";data:"+data);
}while(cur.moveToNext());
cur.close();
}
}
mSQLiteDatabase.close();//關閉資料庫連接
}catch(Exception e){
e.printStackTrace();
}
//deleteDatabase("testDatabase.db");//刪除資料庫
}
});

Ⅱ 請教高手如何使用QT建立SQLite資料庫

#include <QtGui>
#include <QtSql>

#include "scooterwindow.h"

ScooterWindow::ScooterWindow()
{
model = new QSqlTableModel(this);
model->setTable("scooter");
model->setSort(Scooter_Name, Qt::AscendingOrder);
model->setHeaderData(Scooter_Name, Qt::Horizontal, tr("Name"));
model->setHeaderData(Scooter_MaxSpeed, Qt::Horizontal, tr("MPH"));
model->setHeaderData(Scooter_MaxRange, Qt::Horizontal, tr("Miles"));
model->setHeaderData(Scooter_Weight, Qt::Horizontal, tr("Lbs"));
model->setHeaderData(Scooter_Description, Qt::Horizontal,
tr("Description"));
model->select();

view = new QTableView;
view->setModel(model);
view->setSelectionMode(QAbstractItemView::SingleSelection);
view->setSelectionBehavior(QAbstractItemView::SelectRows);
view->setColumnHidden(Scooter_Id, true);
view->resizeColumnsToContents();
view->setEditTriggers(QAbstractItemView::NoEditTriggers);

QHeaderView *header = view->horizontalHeader();
header->setStretchLastSection(true);

QHBoxLayout *mainLayout = new QHBoxLayout;
mainLayout->addWidget(view);
setLayout(mainLayout);

setWindowTitle(tr("Scooters"));
}

#ifndef SCOOTERWINDOW_H
#define SCOOTERWINDOW_H

#include <QWidget>

class QSqlTableModel;
class QTableView;

enum {
Scooter_Id = 0,
Scooter_Name = 1,
Scooter_MaxSpeed = 2,
Scooter_MaxRange = 3,
Scooter_Weight = 4,
Scooter_Description = 5
};

class ScooterWindow : public QWidget
{
Q_OBJECT

public:
ScooterWindow();

private:
QSqlTableModel *model;
QTableView *view;
};

#endif

#include <QtGui>
#include <QtSql>

#include "scooterwindow.h"

bool createConnection()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("scooters.dat");
if (!db.open()) {
QMessageBox::warning(0, QObject::tr("Database Error"),
db.lastError().text());
return false;
}
return true;
}

void createFakeData()
{
QSqlQuery query;
query.exec("DROP TABLE scooter");

query.exec("CREATE TABLE scooter ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"name VARCHAR(40) NOT NULL, "
"maxspeed INTEGER NOT NULL, "
"maxrange INTEGER NOT NULL, "
"weight INTEGER NOT NULL, "
"description VARCHAR(80) NOT NULL)");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Mod-Rad 1500', 40, 35, 298, "
"'Speedometer, odometer, battery meter, turn signal "
"indicator, locking steering column')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Rad2Go Great White E36', 22, 12, 93, "
"'10\" airless tires')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('X-Treme X360', 21, 14, 59, "
"'Cargo rack, foldable')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Vego SX 600', 20, , 76, "
"'Two interchangeable batteries, foldable')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Sunbird E Bike', 18, 30, 118, '')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Leopard Shark', 16, 12, 63, "
"'Battery indicator, removable seat, foldable')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Vego iQ 450', 15, 0, 60, "
"'OUT OF STOCK')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('X-Treme X-11', 15, 11, 38, "
"'High powered brakes, foldable')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('ZZ Cruiser', 14, 10, 46, "
"'Two batteries, removable seat')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('X-Treme X-010', 10, 10, 14, "
"'Solid tires')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Q Electric Chariot', 10, 15, 60, "
"'Foldable')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('X-Treme X250', 15, 12, 0, "
"'Solid aluminum deck')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Go MotorBoard 2000X', 15, 0, 20, "
"'Foldable and carryable')");
query.exec("INSERT INTO scooter (name, maxspeed, "
"maxrange, weight, description) "
"VALUES ('Goped ESR750 Sport Electric Scooter', "
"20, 6, 45, " "'Foldable and carryable')");
}

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

bool create = !QFile::exists("scooters.dat");
if (!createConnection())
return 1;
if (create)
createFakeData();

ScooterWindow window;
window.resize(600, 500);
window.show();

return app.exec();
}

Ⅲ 如何進入QT自待的SQLite資料庫

如何進入QT自待的SQLite資料庫
詳細操作件如下命令adb shell#cd data#cd data#ls 每個應用程序都會在這里根據包名創建一個文件夾#cd org.contact#cd databases#sqlite3 contact //進入資料庫#.schema //查看錶內容

Ⅳ qt 怎麼支持sqlite資料庫

Qt默認是支持QSQLITE資料庫的

QSqlDatabasedb=QSqlDatabase::addDatabase("QSQLITE");
db.setHostName("acidalia");
db.setDatabaseName("customdb");
db.setUserName("mojito");
db.setPassword("J0a1m8");
boolok=db.open();

Ⅳ Qt如何對sqlite資料庫進行加密

給SQLite資料庫加密解密的方法:
1、創建空的sqlite資料庫。
//資料庫名的後綴你可以直接指定,甚至沒有後綴都可以
//方法一:創建一個空sqlite資料庫,用IO的方式
FileStream fs = File.Create(「c:\\test.db「);
//方法二:用SQLiteConnection
SQLiteConnection.CreateFile(「c:\\test.db「);

創建的資料庫是個0位元組的文件。
2、創建加密的空sqlite資料庫
//創建一個密碼為password的空的sqlite資料庫
SQLiteConnection.CreateFile(「c:\\test2.db「);
SQLiteConnection cnn = new SQLiteConnection(「Data Source=c:\\test2.db「);
SQLiteConnection cnn = new SQLiteConnection(「Data Source=D:\\test2.db「);
cnn.Open();
cnn.ChangePassword(「password「);

3、給未加密的資料庫加密
SQLiteConnection cnn = new SQLiteConnection(「Data Source=c:\\test.db「);
cnn.Open();
cnn.ChangePassword(「password「);

4、打開加密sqlite資料庫
//方法一
SQLiteConnection cnn = new SQLiteConnection(「Data Source=c:\\test2.db「);
cnn.SetPassword(「password「);
cnn.Open();
//方法二
SQLiteConnectionStringBuilder builder = new SQLiteConnectionStringBuilder();
builder.DataSource = @」c:\test.db「;
builder.Password = @」password「;
SQLiteConnection cnn = new SQLiteConnection(builder.ConnectionString);
cnn .Open();

除了用上述方法給SQLite資料庫加密以外,您還可以使用專業的文件加密軟體將SQLite資料庫加密。

超級加密 3000採用先進的加密演算法,使你的文件和文件夾加密後,真正的達到超高的加密強度,讓你的加密數據無懈可擊。

超級加密3000使用起來,只要點擊需要加密的文件的右鍵,即可輕松實現文件的加密。

解密只要雙擊已加密文件,輸入密碼即可輕松搞定。

Ⅵ Qt怎麼打開指定文件夾下的SQLite數據文件

首先,你的資料庫肯定是跟著你的程序跑的,你不能指定你程序一定要用戶放到D盤下面,也許用戶喜歡把程序放到E盤下面呢。所以你在指定的時候最好用相對路徑,假設你程序運行目錄是在bin下面,你可以在bin下面創建一個data目錄專門用來保存資料庫,你指定目錄的時候就可以
db.setDatabaseName("./data/student.db");//這樣指定。

Ⅶ qt如何連接sqlite資料庫

#include <QtGui/QApplication>
#include <QSqlDatabase>
#include <QtSql>
#include <QSQLiteDriver>
int main(int argc, char *argv[])
{
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase(new QSQLiteDriver(),"QSQLITE");
db.setDatabaseName("test.db");
//test.db是通過SQLite程序創建的資料庫文件,當前文件夾下
if ( !db.open())
{
return 0;
}
return app.exec();
}

這是打開 後面的操作跟其他的資料庫操作一樣。還不明白可以追問。望採納 謝謝

Ⅷ qt如何調用sqlite資料庫

//自己設置連接名,不使用默認連接名
if(SdbSqlite.contains("Conn2sqlite"))
{
SdbSqlite=QSqlDatabase::database("Conn2sqlite");
}
else
{
SdbSqlite=QSqlDatabase::addDatabase(
"QSQLITE","Conn2sqlite");
}
SdbSqlite.setDatabaseName("FiveChess.database");//資料庫名稱

這是連接

if(SdbSqlite.open())
{
intRltRowNumber=0;//查詢出來的結果列數
QStringStrSql1=
QString("selectname,grade,integration,round,victory,defeat,rank"
"frommemberwhereaccount='%0'andpassword='%1';")
.arg(StrAcn,StrPsdOrNew);
QSqlQueryquery1(SdbSqlite);
query1.exec(StrSql1);
while(query1.next())
{
if(SdbSqlite.driver()->hasFeature(QSqlDriver::QuerySize))
{
//速度快
RltRowNumber=query1.size();
}
else
{
//速度很慢
//實際執行的是這個
query1.last();
RltRowNumber=query1.at()+1;
}
}
SdbSqlite.close();
}

這是查詢

熱點內容
python導入csv數據 發布:2024-11-17 13:38:57 瀏覽:32
質量m的演算法 發布:2024-11-17 13:37:24 瀏覽:888
php讀取網頁 發布:2024-11-17 13:29:30 瀏覽:861
安卓服光遇夏日活動什麼時候結束 發布:2024-11-17 13:23:53 瀏覽:31
電腦網路伺服器機主名 發布:2024-11-17 13:22:13 瀏覽:149
手機存儲設備沒了怎麼辦 發布:2024-11-17 13:20:33 瀏覽:426
保護生態最新腳本 發布:2024-11-17 13:17:34 瀏覽:1
解腳本工具 發布:2024-11-17 13:12:02 瀏覽:822
編譯器優化有幾種 發布:2024-11-17 13:11:58 瀏覽:387
vbnet判斷文件夾是否存在 發布:2024-11-17 13:09:00 瀏覽:773