searchusermenu
  • 发布文章
  • 消息中心
点赞
收藏
评论
分享
原创

如何使用Python在SQL数据库中创建表

2023-09-21 10:03:15
1
0

使用Python在SQL数据库中创建表:

1、首先使用模块的方法建立与sqllite3

import sqlite3

# connect to the database
conn = sqlite3.connect('example.db')

2、然后根据该方法建立一个游标对象cursor

# create a cursor object
c = conn.cursor()

3、接下来根据执行SQL查询以名为employees三列的表:id、name和age,id设置为主键。

# create a table
c.execute(' ' 'CREATE TABLE employees

                    (id INT PRIMARY KEY NOT NULL,

                     name TEXT NOT NULL,

                     age INT NOT NULL)' ' ')

4、创建后使用方法保存更改commit。
# save the changes
conn.commit( )

5、最后使用close进行关闭。
# close the connection
conn.close( )

0条评论
作者已关闭评论
2****m
2文章数
0粉丝数
2****m
2 文章 | 0 粉丝
2****m
2文章数
0粉丝数
2****m
2 文章 | 0 粉丝
原创

如何使用Python在SQL数据库中创建表

2023-09-21 10:03:15
1
0

使用Python在SQL数据库中创建表:

1、首先使用模块的方法建立与sqllite3

import sqlite3

# connect to the database
conn = sqlite3.connect('example.db')

2、然后根据该方法建立一个游标对象cursor

# create a cursor object
c = conn.cursor()

3、接下来根据执行SQL查询以名为employees三列的表:id、name和age,id设置为主键。

# create a table
c.execute(' ' 'CREATE TABLE employees

                    (id INT PRIMARY KEY NOT NULL,

                     name TEXT NOT NULL,

                     age INT NOT NULL)' ' ')

4、创建后使用方法保存更改commit。
# save the changes
conn.commit( )

5、最后使用close进行关闭。
# close the connection
conn.close( )

文章来自个人专栏
文章 | 订阅
0条评论
作者已关闭评论
作者已关闭评论
0
0