sqlite如何创建表
时间: 2024-06-06 11:08:57 浏览: 5
在 SQLite 中创建表需要使用 CREATE TABLE 语句,语法如下:
CREATE TABLE table_name (
column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype,
....
);
其中,table_name 为表的名称,column1、column2、column3 代表表中的列,datatype 代表列的数据类型。PRIMARY KEY 是一个关键字,表示该列是主键。
相关问题
qt sqlite创建表
以下是在Qt中连接SQLite数据库并创建用户表、心愿表、评论表的示例代码:
```cpp
#include <QCoreApplication>
#include <QSqlDatabase>
#include <QSqlQuery>
#include <QDebug>
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
// 连接SQLite数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("mydatabase.db");
if (!db.open()) {
qDebug() << "Failed to connect to database.";
return -1;
}
// 创建用户表
QSqlQuery query;
query.exec("CREATE TABLE users (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER)");
// 创建心愿表
query.exec("CREATE TABLE wishes (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, content TEXT)");
query.exec("CREATE INDEX idx_wishes_user_id ON wishes (user_id)");
// 创建评论表
query.exec("CREATE TABLE comments (id INTEGER PRIMARY KEY AUTOINCREMENT, user_id INTEGER, wish_id INTEGER, content TEXT)");
query.exec("CREATE INDEX idx_comments_user_id ON comments (user_id)");
query.exec("CREATE INDEX idx_comments_wish_id ON comments (wish_id)");
qDebug() << "Tables created successfully.";
return a.exec();
}
```
这段代码首先使用`QSqlDatabase`类连接SQLite数据库,然后使用`QSqlQuery`类执行SQL语句来创建用户表、心愿表、评论表。其中,用户表包含id、name、age三个字段,心愿表包含id、user_id、content三个字段,评论表包含id、user_id、wish_id、content四个字段。在创建心愿表和评论表时,还创建了相应的索引以提高查询效率。
sqlite 创建表的sql语句
SQLite的CREATE TABLE语句用于在给定的数据库中创建一个新表。创建基本表涉及到命名表和定义每一列的数据类型。CREATE TABLE语句的基本语法如下:
CREATE TABLE table_name(
column1 datatype,
column2 datatype,
column3 datatype,
...
);
在SQLite中,创建表的完整代码示例如下:
import sqlite3
conn = sqlite3.connect('mysqlite.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS table_name(
column1 datatype,
column2 datatype,
column3 datatype,
...
)''')
conn.commit()
conn.close()
其中,table_name是要创建的表的名称,column1、column2、column3等分别是表的列名,datatype是每一列的数据类型。
请根据需要修改table_name、column1、column2、column3和datatype的值,以适应你的具体情况。<span class="em">1</span><span class="em">2</span><span class="em">3</span>
#### 引用[.reference_title]
- *1* [SQLite 创建表](https://download.csdn.net/download/weixin_38606870/13704462)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
- *2* *3* [【Sqlite3】创建数据库、创建表](https://blog.csdn.net/weixin_40458518/article/details/122170345)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"]
[ .reference_list ]
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)