如何使用 sqlite3 和 Python [英] How to work with sqlite3 and Python
问题描述
我正在寻找任何使用 Python 的数据库解决方案.并找到了教程 Python:一个简单的分步 SQLite 教程.在那里我找到了一个代码示例,它展示了如何创建数据库和插入一些数据:
I was looking for any database solution with Python. And found the tutorial Python: A Simple Step-by-Step SQLite Tutorial. There I found a code example which shows how to create a database and INSERT some data:
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
的数据库.如何创建该数据库?
- If I want to use
sqlite3
do I need to install any particular Python modules? - In the above code I can see one database named
mydatabase.db
. How do I create that database?
如果有人帮助我从脑海中清除这些困惑,我可以给这些新模块一个良好的开端.
If anyone help me to get these confusions cleared from my head, I can give these new module a good start.
谢谢
推荐答案
您不需要(安装)任何额外的 Python 模块来使用 sqlite3.
You don't need (to install) any additional Python modules to use sqlite3.
如果数据库不存在,通常会在与脚本相同的目录中自动创建.
If the database doesn't exist, it will be automatically created usually in the same directory as the script.
在运行你的脚本时,我得到了这个:-
On running your script, I get this :-
$ 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, artist text, release_date text,
publisher text, media_type text)
sqlite>
这篇关于如何使用 sqlite3 和 Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!