当前位置:首页 » 操作系统 » 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();
}

这是查询

热点内容
php包下载地址 发布:2024-11-17 11:32:07 浏览:136
使用access数据库 发布:2024-11-17 11:23:49 浏览:774
易语言自动喊话源码 发布:2024-11-17 11:13:11 浏览:588
virtualbox虚拟机共享文件夹 发布:2024-11-17 11:12:25 浏览:424
电脑开机密码忘记了如何恢复 发布:2024-11-17 11:10:48 浏览:320
如何开启微信支付密码错误 发布:2024-11-17 10:59:02 浏览:500
数列的c语言编程 发布:2024-11-17 10:33:50 浏览:136
服务器换ip多久生效 发布:2024-11-17 10:33:49 浏览:387
ipad导出缓存b站视频 发布:2024-11-17 10:29:33 浏览:356
mc手机版服务器搭建 发布:2024-11-17 10:23:38 浏览:55