qt连接并操作sqlite

#include <QtCore/QCoreApplication>

#include <QtSql>
#include <QTextCodec>


int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);
    QTextCodec::setCodecForTr(QTextCodec::codecForLocale());
    QSqlDatabase dbconn=QSqlDatabase::addDatabase("QSQLITE");
    dbconn.setDatabaseName("mytest1.db");
    if(!dbconn.open()){
        qDebug()<<QObject::tr("打开数据库错误!");
    }else{
        qDebug()<<QObject::tr("打开数据库成功!");
    }
    QSqlQuery query;
    //query.exec("create table table3(id INTEGER PRIMARY KEY AUTOINCREMENT,name varchar(20));");
    //query.exec("delete from table2 where id = 0;");
    //query.exec("drop table table1;drop table table2;");
    query.exec(QObject::tr("insert into table3 (name) values('22');"));
    query.exec("select * from table3;");
    while(query.next()){
        int ele0=query.value(0).toInt();
        QString ele1=query.value(1).toString();
        qDebug()<<ele0<<ele1;
    }
    query.exec("drop table3");
    return a.exec();
}

posted on 2012-09-16 16:54  卑鄙De小贝  阅读(683)  评论(0)    收藏  举报

导航