如何使用sqlite3和Python

我正在寻找使用 Python的任何数据库解决方案.并找到了教程 Python: A Simple Step-by-Step SQLite Tutorial.在那里我找到了一个代码示例,它展示了如何创建数据库并插入一些数据:

import sqlite3

conn = sqlite3.connect("mydatabase.db") # or use :memory: to put it in RAM

cursor = conn.cursor()

# create a table
cursor.execute("""CREATE TABLE albums
                  (title text,artist text,release_date text,publisher text,media_type text) 
               """)

我是sqlite3的新手.

>如果我想使用sqlite3,我是否需要安装任何特定的Python模块?
>在上面的代码中,我可以看到一个名为mydatabase.db的数据库.如何创建该数据库?

如果有人帮助我解决这些困惑,我可以给这些新模块一个良好的开端.

谢谢

解决方法

您不需要(安装)任何其他Python模块来使用sqlite3.

如果数据库不存在,则通常会在与脚本相同的目录中自动创建该数据库.

在运行脚本时,我得到: –

$ls *.db
ls: *.db: No such file or directory

$python test.py

$ls *.db
mydatabase.db

$sqlite3 mydatabase.db 
SQLite version 3.7.7 2011-06-25 16:35:41
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> select * from sqlite_master;
table|albums|albums|2|CREATE TABLE albums
             (title text,media_type text)
sqlite>

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能