如何打开Python 3安装的sqlite3?

时间:2015-11-13 11:23:22

标签: python python-3.x sqlite

Ubuntu python2和python3都可以导入sqlite3,但是我不能在命令提示符下键入sqlite3来打开它,它说没有安装sqlite3,如果我想用它来运行python我应该单独安装sqlite3使用apt-get或者我可以在python的某个目录中找到它,将它添加到路径并直接在命令行中使用。

我还在mac上安装了python3.5,python2附带了mac,我可以在sqlite3类型的命令行中使用sqlite3,它是版本3.8.10.2,似乎是由python2安装的,但是python3 .5安装了不同版本的sqlite3,在哪里可以找到它?

3 个答案:

答案 0 :(得分:3)

你不需要在python上安装任何可以使用sqlite3的东西。

关于sqlite:https://www.sqlite.org/about.html

如果您有使用数据库的经验, 你可以认为sqlite3是一个像数据库包含表格的文件。

因为python支持sqlite3,所以你可以创建一个新的sqlite3文件。

这个例子创建一个新的example.db文件,如果不存在,只使用python。

import sqlite3
conn = sqlite3.connect('example.db')
c = conn.cursor()

# Create table
c.execute('''CREATE TABLE stocks
             (date text, trans text, symbol text, qty real, price real)''')

# Insert a row of data
c.execute("INSERT INTO stocks VALUES ('2006-01-05','BUY','RHAT',100,35.14)")

# Save (commit) the changes
conn.commit()

# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
conn.close()

阅读此文档:https://docs.python.org/2/library/sqlite3.html

但我建议您安装sqlite以使用Commandite Shell For SQLite。

$ sqlite3 ex1
SQLite version 3.8.5 2014-05-29 12:36:14
Enter ".help" for usage hints.
sqlite> create table tbl1(one varchar(10), two smallint);
sqlite> insert into tbl1 values('hello!',10);
sqlite> insert into tbl1 values('goodbye', 20);
sqlite> select * from tbl1;
hello!|10
goodbye|20
sqlite>

答案 1 :(得分:0)

正如Robert Moon所说,自2.5版以来,sqlite3已包含在Python中,您可以直接使用它。看他的帖子有一个例子。

Sqlite是一个管理存储在文件中的SQL数据库的库。因此,很容易在软件或网站中使用它,使用可用于向数据库发出请求的语言(存储在文件中)。

如果你想在python脚本之外使用sqlite3,在Ubuntu中,你必须安装包

sudo apt-get install sqlite3

然后你可以运行sqlite终端

sqlite3

发出请求,创建新数据库等

答案 2 :(得分:0)

任何发现自己的Cygwin用户:运行Cygwin安装.exe ...

选择"类别","数据库",然后一个项目说" sqlite3客户端访问sqlite3数据库",或者说这个效果的单词。