Pong
June 21th 2014, Qt SQLite Database Manipulations
← June 21th 2014 SQLite Statements | ● | June 23th 2014 SSL Transmission Acknowledgement →
Create a table with Qt using the default db connection:
// create key/value table
bool createTable()
{
// create table "files"
QString create("CREATE TABLE files"
"("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"tid TEXT NOT NULL,"
"uid TEXT NOT NULL,"
"isotime VARCHAR(19),"
"content BLOB,"
"compressed BIT,"
")");
QString create_search_index("CREATE UNIQUE INDEX search_index "
"ON files(uid, tid)");
QSqlQuery query;
bool success = query.exec(create));
success &= query.exec(create_search_index);
return(success);
}
bool createTable()
{
// create table "files"
QString create("CREATE TABLE files"
"("
"id INTEGER PRIMARY KEY AUTOINCREMENT,"
"tid TEXT NOT NULL,"
"uid TEXT NOT NULL,"
"isotime VARCHAR(19),"
"content BLOB,"
"compressed BIT,"
")");
QString create_search_index("CREATE UNIQUE INDEX search_index "
"ON files(uid, tid)");
QSqlQuery query;
bool success = query.exec(create));
success &= query.exec(create_search_index);
return(success);
}
Insert into the table with Qt:
// write a file to the db
void write(QString tid, QString uid, QDateTime isotime, QByteArray &data)
{
QString insert=QString("INSERT OR REPLACE INTO files VALUES(NULL, '%1', '%2', '%3', ?)")
.arg(tid).arg(uid).arg(isotime.toString(Qt::ISODate)));
QSqlQuery query;
query.prepare(insert);
query.addBindValue(data);
query.exec());
}
void write(QString tid, QString uid, QDateTime isotime, QByteArray &data)
{
QString insert=QString("INSERT OR REPLACE INTO files VALUES(NULL, '%1', '%2', '%3', ?)")
.arg(tid).arg(uid).arg(isotime.toString(Qt::ISODate)));
QSqlQuery query;
query.prepare(insert);
query.addBindValue(data);
query.exec());
}
← June 21th 2014 SQLite Statements | ● | June 23th 2014 SSL Transmission Acknowledgement →