开发者

How to create tables in sqlite 3?

开发者 https://www.devze.com 2023-01-28 14:22 出处:网络
I just downloaded sqlite3.exe. It opens up as a command prompt. I created a table test & inserted a few entries in it. I used .backup test just in case. After I exit the program using .exit and re

I just downloaded sqlite3.exe. It opens up as a command prompt. I created a table test & inserted a few entries in it. I used .backup test just in case. After I exit the program using .exit and reopened it I don't find the table listed under .tables nor can I run any query on it.

I need to quickly run an open source python program that makes use of this table 开发者_JAVA技巧& although I have worked with MySQL, I have no clue about sqlite. I need the minimal basics of sqlite. Can someone guide me through this or at least tell me how to permanently store my tables.

I have put this sqlite3.exe in Python folder assuming that python would then be able to read the sqlite files. Any ideas on this?


sqlite is built in to Python.
You should be able to access your table like this:

import sqlite3
conn = sqlite3.connect('/path/to/my.db')
curs = conn.cursor()
curs.execute("SELECT a_column FROM my_table;").fetchone()
curs.close()
conn.close()

You can execute your DDL statements from Python as well.
Make sure to commit the changes.

curs.execute("CREATE TABLE my_table (a_column text);")
conn.commit()


Why did you download some sqlite3.exe at all? Python should ship with sqlite3 already on board. import sqlite3 is all you need to do as long as you have a recent Python distribution. To your problem: I would guess that sqlite3 creates a table in memory by default. Using Python, you need to dbConn = sqlite3.connect("somefile") to connect to a database. Then you can use dbCursor = dbConn.cursor() to connect to this file. The cursor can execute SQL commands by calling its execute(command) method. For example: dbConn.execute("create table test (row text, otherrow real)") Finally, you need to call dbConn.commit() to save everything you changed in the database. The Python documentation knows everything else: http://docs.python.org/library/sqlite3.html


Just execute sqlite3 foo.db? This will permanently store everything you do afterwards in this file. (No need for .backup.)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号