添加qtsql的庫
連接數據庫
QSqlDatabase_db = QSqlDatabase::addDatabase("QODBC");
_db.setHostName();
_db.setDatabaseName();
_db.setUserName();
_db.setPassword();
_db.setPort();
_db.open();
增
QSqlQuery query;
query.prepare("INSERT INTO xx (xx,xx,xx,xx,xx,xx) VALUES (?,?,?,?,?,?)");
query.addBindValue(xx);
query.addBindValue(xx);
query.addBindValue(txx);
query.addBindValue(xx);
query.addBindValue(xx);
query.addBindValue(xx);
query.exec();
刪
QSqlQuery query;
query.prepare("DELETE FROM xx WHERE xx = ?");
query.addBindValue(xx);
query.exec();
改
QSqlQuery query;
query.prepare("UPDATE xx SET xx = ?,xx = ?,xx = ?,xx = ?,xx = ? WHERE xx = ?");
query.addBindValue(xx);
query.addBindValue(xx);
query.addBindValue(xx);
query.addBindValue(xx);
query.addBindValue(xx);
query.addBindValue(xx);
query.exec());
查
QSqlQuery query;
query.exec("SELECT * FROM xx");
while (query.next()){
query.value(0);
query.value(1);
刪除表
QSqlQuery query;
query.exec("DROP TABLE xx);
創建表
QString sql;
sql = "CREATE TABLE xx (";
sql += "xx int NOT NULL PRIMARY KEY,";
sql += "xx varchar(64) DEFAULT NULL,";
sql += "xx int DEFAULT NULL,";
sql += "xx numeric(15,0) DEFAULT NULL,";
sql += "xx int DEFAULT NULL,";
sql += "xx int DEFAULT NULL)";
query.prepare(sql);
query.exec();
原文來自微信公眾號"程序員成長日志",已經工作的程序員朋友可以關注下,分享日常工作中解決的問題即可賺取稿費,大家一起成長~

