Qt 操作sql server数据库

Qt 操作sql server数据库

添加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();
posted @ 2024-03-27 17:05  伟大的厨师  阅读(66)  评论(0编辑  收藏  举报