#include <QCoreApplication>#include<QDebug>#include<QSqlDatabase>#include<QSqlQuery>intMainintargcChar*argv[]) {Qcoreapplication A (argc, argv); Qsqldatabase DB= Qsqldatabase::adddatabase ("Qsqlite"); Db.setdatabasename ("student.db"); if(!Db.open ()) {Qdebug ()<<"Open Err"; } Else{qdebug ()<<"database is OK"; } qsqlquery query (db); BOOLisOK = Query.exec ("CREATE TABLE stud (ID int primary key,name varchar )"); Query.exec ("INSERT INTO stud values (111, ' liming ')"); Query.exec ("INSERT INTO stud values (102, ' Li ')"); Query.exec ("INSERT INTO stud values (103, ' Lisi ')"); Query.exec ("INSERT INTO stud values ("Zhangsan")"); if(!isOK) {Qdebug ()<<"Table Err"; } Else{qdebug ()<<"table is OK"; } query.exec ("select * FROM Stud"); while(Query.next ()) {Qdebug ()<<"ID:"<<query.value (0). ToInt () <<"Name:"<<query.value (1). toString (); } returna.exec ();}
QT Connection Database SQLite