我能够创建数据库文件,但无法创建表。我想通过Python而不是终端运行所有内容。我已经通过终端安装了SQLite。我使用的是MacOS(High Sierra)
#THIS PYTHON FILE CREATES THE DATABASE
import sqlite3
#Creates the Database File
sqlite_file = '/Users/rebecca/Desktop/Movie Database/MovieDatabase '
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
#closes the database door after completeing operations 'conn.close()'
#make changes or updates and save to database conn.commit()''conn.close()'
#THIS PYTHON FILE SHOULD CREATE THE TABLE IN THE DATABASE
import sqlite3
#Connecting to the database file
sqlite_file = '/Users/rebecca/Desktop/Movie Database/MovieDatabase '
conn = sqlite3.connect(sqlite_file)
c = conn.cursor()
db = sqlite3.connect('MovieDatabase .db')
CREATE TABLE movietable (movie TEXT REQUIRED, actor TEXT REQUIRED, rating INTEGER, viewed BOOLEAN, watchagain BOOLEAN);
#CREATE A TABLE IN THE MOVIE DATABASE
答案 0 :(得分:0)
您需要执行以下语句:
c.execute("CREATE TABLE movietable (movie TEXT REQUIRED, actor TEXT REQUIRED, rating INTEGER, viewed BOOLEAN, watchagain BOOLEAN)")
conn.commit()
此外,如果表已经存在,您可以使用sqlite3命令“如果不存在则创建表”来防止错误(或者,如果希望自己处理该错误而不是忽略它,则可以使用Python try块)。 / p>
您还应该考虑为表创建一个主键,这样您就不会多次插入同一部电影了-也许也为电影创建了一个ID。