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()<<QObject::tr ("数据库表创建成功\n");
      else

qDebug()<<QObject::tr ("数据库创建失败\n");





 //插入到数据库中
        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()< ()<<QString(QObject::tr ("插入失败\n"));
 
   
         }
         else
         {
             qDebug()<<QObject::tr ("插入成功\n");
         }

你可能感兴趣的:(Qt的学习)