#define DB_SALES_DRIVER "QMYSQL3"
#define DB_SALES_DBNAME "CAAS"
#define DB_SALES_USER "root"
#define DB_SALES_PASSWD ""
#define DB_SALES_HOST ""
main.cpp
#include <qapplication.h>
#include <qsqldatabase.h>
#include <qsqlquery.h>
#include <qsqlcursor.h>
#include <qtextedit.h>
#include <qstring.h>
#include <qtextcodec.h>
#include <qvbox.h>
#include "data.h"
#include <stdio.h>
#include <qpushbutton.h>
class MyQVBox : public QVBox
{
public:
MyQVBox( QWidget *parent=0, const char *name=0 )
//public slots:
// refValue(QSrting lzw)
}
MyQVBox::MyQVBox( QWidget *parent, const char *name )
: QVBox( parent, name )
{
QString Lzw("\n")
QSqlDatabase *defaultDB = QSqlDatabase::addDatabase(DB_SALES_DRIVER)
if ( defaultDB )
{
defaultDB->setDatabaseName( DB_SALES_DBNAME )
defaultDB->setUserName( DB_SALES_USER )
defaultDB->setPassword( DB_SALES_PASSWD )
defaultDB->setHostName( DB_SALES_HOST )
if ( defaultDB->open() )
{
//插入数据
QSqlQuery query("INSERT INTO test(ID,Name,Age) VALUES(1155, 'Ginger', 125)" )
//提取数据
QSqlCursor cur( "test" ) // 指定表/视图名称
cur.select() // 我们将检索每一条记录
while ( cur.next() )
{
qDebug( cur.value( "ID" ).toString() + ": " +
cur.value( "Name" ).toString() + " " +
cur.value( "Age" ).toString() )
Lzw.append(cur.value( "ID" ).toString() + ": " +
cur.value( "Name" ).toString() + " " +
cur.value( "Age" ).toString()+"\n")
}
//qDebug(Lzw)
printf("aaaaaaa")
}
}
QString ustr = (QTextCodec::codecForLocale())->toUnicode(Lzw)
QString sstr = (QTextCodec::codecForLocale())->toUnicode("刷新")
QTextEdit *myEdit = new QTextEdit(this,0)
myEdit->setText(ustr)
QPushButton *quit = new QPushButton(sstr, this, "quit" )
connect( quit, SIGNAL(clicked()), qApp, SLOT(quit()) )
}
int main( int argc, char *argv[] )
{
QApplication app( argc, argv )
MyQVBox *mybox = new MyQVBox(0,0)
//mybox->setText(ustr)
app.setMainWidget(mybox)
mybox->show()
return app.exec()
}
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE")db.setDatabaseName("sqluser.db")
db.open()
QString sql("select * from quanbu")
QSqlQueryModel *model = new QSqlQueryModel()
model->setQuery(sql, db)
if (model->lastError().isValid()) {
QDebug() <<"Query Error: " <<qPrintable(model->lastError().text())
delete model
model = 0
} else {
//输出model数据
}
//close database
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)