Qt创建数据库、插入内容

//创建数据库

QSqlDatabase db=QSqlDatabase::addDatabase ("QSQLITE");

hostName = config.getDatabaseHost();

databaseName = config.getDatabaseName();

userName = config.getDatabaseUsername();

password = config.getDatabasePassword();


db.setHostName (hostName);

db.setDatabaseName (databaseName);

db.setUserName (userName);

db.setPassword (password);

db.open ();

QSqlQuery query;

bool success=query.exec("create table AGVmessage(Num int primary key,ipaddress varchar)");

if(success)

qDebug()<
else

【Qt创建数据库、插入内容】qDebug()<



//插入到数据库中

QSqlQuery query;


query.prepare("insert into AGVMessage(Num,ipaddress)values(:Num,:ipaddress)");

query.bindValue(":Num",click);

query.bindValue (":ipaddress",ipAddress);

bool ni= query.exec();

if(!ni)

{

QSqlError lastError=query.lastError ();

qDebug()<

}

else

{

qDebug()<
}


    推荐阅读