SQL教程(W3school)
SQL教程(菜鸟)
Qt中建立Sqlite使用环境的两种方式)
QT中读写Sqlite数据库的三种方式
Qt中操作SQLite数据库
(sqlite在进行字符串查询的时候,默认是区分大小写)
不区分大小写的三种解决方案:
一:使用大小写转换函数LOWER,UPPER
select * from test where LOWER(name) = 'abcde';
二:在进行比较时强制声明不区分大小写
select * from test where name = 'abcde' collate nocase;
三:创建表时声明该字段不区分大小写
create table test (id integer, name text collate nocase)
注:
(1)不推荐使用方案一,有额外的性能消耗
(2)如果只是少量查询不需要区分大小写,建议使用方案二
(3)在任何情况下都不需要区分大小写,方案三完美献身
delete from TableName; //清空数据
update sqlite_sequence SET seq = 0 where name ='TableName';//自增
【SQLiteStudio】的下载及安装
SQLite可视化工具GUI/SQLiteExpert/SQLiteStudio/SQLiteBrowser
MBTiles 是一种地图瓦片存储的数据规范,可大大提高海量地图瓦片的读取速度,比通过瓦片文件方式的读取要快很多,适用于Android、IPhone等智能手机的离线地图存储。
MBTiles通用方法是将瓦片表分成两张:一个用来存储原始图像和一个存储瓦片坐标对应那些图片。具体设计如下:map表存储瓦片行列号以及对应的瓦片ID;images表对瓦片进行存储;视图tiles是基于map和images生成的。
#include "mainwindow.h"
#include
#include
#include
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
// Set up database
QString dbName("myDatabase.db3"); //默认路径在文件夹build-sqldemo-Desktop_Qt_5_12_9_MSVC2017_64bit-Debug
QFile::remove(dbName); // delete sqlite file if it exists from a previous run
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(dbName);
db.open();
QSqlQuery query = QSqlQuery(db);
query.exec("CREATE TABLE IF NOT EXISTS imgTable ( filename TEXT, imagedata BLOB )");
// Generate an image (in this case a screenshot) and store it in a QByteArray
QScreen *screen = a.primaryScreen();
QPixmap inPixmap = screen->grabWindow(0);
QByteArray inByteArray;
QBuffer inBuffer(&inByteArray);
inBuffer.open(QIODevice::WriteOnly);
inPixmap.save(&inBuffer, "PNG"); // write inPixmap into inByteArray in PNG format
// Alternatively, load an image file directly into a QByteArray
// QFile file("screenshot.png");
// if (!file.open(QIODevice::ReadOnly)) return;
// QByteArray inByteArray = file.readAll();
// Insert image bytes into the database
// Note: make sure to wrap the :placeholder in parenthesis
query.prepare("INSERT INTO imgTable (filename, imagedata) VALUES ('screenshot.png', :imageData)");
query.bindValue(":imageData", inByteArray);
if (!query.exec())
qDebug() << "Error inserting image into table:\n"
<< query.lastError();
// Get image data back from database
if (!query.exec("SELECT imagedata from imgTable"))
qDebug() << "Error getting image from table:\n"
<< query.lastError();
query.first();
QByteArray outByteArray = query.value(0).toByteArray();
QPixmap outPixmap = QPixmap();
outPixmap.loadFromData(outByteArray);
db.close();
// Display image
QLabel myLabel;
myLabel.setPixmap(outPixmap);
myLabel.show();
//MainWindow w;
//w.show();
return a.exec();
}
QFile file(fileName);
if (!file.open(QIODevice::ReadOnly))
return;
QByteArray byteArray = file.readAll();
QSqlQuery query;
query.prepare("INSERT INTO imgtable (imgdata) VALUES (?)");
query.addBindValue(byteArray);
query.exec();
从JPG文件写入BLOB 代码如下:
with open(“xxxx.jpg","rb") as f:
img = f.read()
ba = QByteArray()
bf = QBuffer(ba)
bf.open(QIODevice.WriteOnly)
bf.write(img)
//这里 data 是BLOB 类型,type 数据类型,我想以后加入PDF,或其它文件
sql = "insert into voucher (type,data) values('JPG',:pic)"
self.query.prepare(sql)
self.query.bindValue(":pic", ba )
self.query.exec()
BLOB到 Qpixmap:
sql = "select data from voucher where ID= ? "
// key 是关键字
query = QSqlQuery()
query.prepare(sql)
query.bindValue(0, key)
query.exec_()
query.first()
ba = QByteArray()
ba.append(query.value(0))
bf = QBuffer(ba)
bf.open(QIODevice.ReadOnly)
imgreader = QImageReader(bf)
img = QImage(imgreader.read())
pix = QPixmap(img)
void CDBMgr::openDb()
{
//建立数据库
if(QSqlDatabase::contains("qt_sql_default_connection"))
{
m_db = QSqlDatabase::database(m_strConnectName);
}
else
{
if(!QSqlDatabase::contains("QSQLITE"))
{
m_db = QSqlDatabase::addDatabase("QSQLITE", m_strConnectName);
m_db.setDatabaseName(m_strDbPath);
}
}
//打开数据库
if(m_db.open())
{
qDebug()<< "successed to open db !";
}
else
{
qDebug()<< "failed to open db ...";
}
//查询表是否存在,没有则创建表STREERMAP(注意:字段名'',类型小写,最后加个;)---
QSqlQuery query = m_db.exec("SELECT count(*) FROM sqlite_master WHERE type='table' AND name = 'STREETMAP'");
int iCount = 0;
while(query.next())
{
iCount = query.value(0).toInt();
}
if(iCount == 0)
{
m_db.exec("CREATE TABLE IF NOT EXISTS STREETMAP("
"'X' integer NOT NULL,"
"'Y' integer NOT NULL,"
"'Z' integer NOT NULL,"
"'TILE' blob NOT NULL"
");");
}
}
void CDBMgr::closeDb()
{
m_db.close();
QSqlDatabase::removeDatabase(m_strConnectName);
}
QSqlDatabase只能在创建它的线程中使用
qt多线程操作数据库(创建不同连接)
Qt数据库之数据库连接池
QT实现sqlite数据库连接池