qt SQLite语句
时间: 2023-07-24 17:35:02 浏览: 171
Qt 中可以使用 QSqlQuery 类来执行 SQLite 数据库的 SQL 语句。下面是一些常用的 SQLite 语句及其 Qt 实现:
1. 创建表
```sql
CREATE TABLE table_name (
column1 datatype,
column2 datatype,
column3 datatype,
.....
);
```
```c++
QSqlQuery query;
query.exec("CREATE TABLE table_name ("
"column1 datatype,"
"column2 datatype,"
"column3 datatype,"
"....."
")");
```
2. 插入数据
```sql
INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);
```
```c++
QSqlQuery query;
query.prepare("INSERT INTO table_name (column1, column2, column3, ...) VALUES (?, ?, ?, ...)");
query.addBindValue(value1);
query.addBindValue(value2);
query.addBindValue(value3);
// ...
query.exec();
```
3. 查询数据
```sql
SELECT column1, column2, column3, ... FROM table_name WHERE condition;
```
```c++
QSqlQuery query;
query.exec("SELECT column1, column2, column3, ... FROM table_name WHERE condition");
while(query.next()) {
QString column1 = query.value(0).toString();
QString column2 = query.value(1).toString();
QString column3 = query.value(2).toString();
// ...
}
```
4. 更新数据
```sql
UPDATE table_name SET column1 = value1, column2 = value2, ... WHERE condition;
```
```c++
QSqlQuery query;
query.prepare("UPDATE table_name SET column1 = ?, column2 = ?, ... WHERE condition");
query.addBindValue(value1);
query.addBindValue(value2);
// ...
query.exec();
```
5. 删除数据
```sql
DELETE FROM table_name WHERE condition;
```
```c++
QSqlQuery query;
query.exec("DELETE FROM table_name WHERE condition");
```
以上就是一些常用的 SQLite 语句及其在 Qt 中的实现方式。需要注意的是,在执行 SQL 语句时,应该使用 prepare 和 addBindValue 函数来避免 SQL 注入攻击。
阅读全文