Qt 创建 SQlite数据库
void Widget::initDB()
{
QSqlDatabase database;
database = QSqlDatabase::addDatabase("QSQLITE");
database.setDatabaseName("test.sqlite3");
if(!database.open())
{
qDebug() << "Error: Failed to connect database." << database.lastError();
}
else
{
qDebug() << "Succeed to connect database.";
}
QSqlQuery sql_query = database.exec("DROP TABLE student");
if(!sql_query.exec("create table student(UserId int primary key, UserName text, PassWord text)"))
{
qDebug() << "Error: Fail to create table." << sql_query.lastError();
}
else
{
qDebug() << "Table created!";
}
if(!sql_query.exec("INSERT INTO student VALUES(1, 'AppleCai', '23')"))
{
qDebug() << "Error: Fail to create table." << sql_query.lastError();
}
else
{
qDebug() << "add one created!";
}
QStringList names;
names <<