1. queryMenu.prepare("insert into ordering (desknum,foodname,foodprice) values(:desknumm,:foodname,:foodprice)"); //準(zhǔn)備執(zhí)行SQL查詢 queryMenu.bindValue(":desknumm", nid); //在占位符上確定綁定的值 queryMenu.bindValue(":foodname", foodnm); queryMenu.bindValue(":foodprice",fdprice); queryMenu.exec(); 2. QSqlQuery query; query.exec("select * from employment where username="+str[0]); query.exec("select * from employment where username=?"); query.bindValue(0,str[0]); //str[0]存放的是從界面獲取的用戶名 query.exec(); 3. bool createConnection() { QSqlDatabase db=QSqlDatabase::addDatabase("QMYSQL"); //連接數(shù)據(jù)庫(kù)驅(qū)動(dòng) db.setUserName("zzp"); db.setPassword("zzp"); db.setHostName("localhost"); //數(shù)據(jù)庫(kù)所在主機(jī)的IP,,此處數(shù)據(jù)庫(kù)保存在本地 db.setDatabaseName("ordersystem");//數(shù)據(jù)庫(kù)名 db.setPort(3306); //端口號(hào) if(!db.open()) { qDebug()<<"Failed to connect to root mysql admin"; } else { qDebug()<<"open database successful!"; return true; } }
|
|