我在SQLite中使用sqlite3和DB浏览器。我的代码:
def createuser(user,password,dbname):
sql_insert = "INSERT INTO login VALUES
('{user}','{password}')".format(user=user, password=password)
print sql_insert
conn = sqlite3.connect(dbname)
c = conn.cursor()
c.execute(sql_insert)
# Save (commit) the changes
conn.commit()
但是我收到数据库被锁定的错误。我不知道该怎么办,因为它昨天正在工作。当我在数据库浏览器中执行SQL代码以显示表“登录”中的值时,这些值就是这些值所在的位置,有时我会在此程序中输入值,有时它们不会出现。吨。这有点来自下面发布的较长代码,但错误消息仅涉及此功能。
import sqlite3
DEBUG = True
# sets up database
def setup():
conn = sqlite3.connect('Practice.db')
c = conn.cursor()
c.execute('''CREATE TABLE login
(Username text, Password text )''')
conn.commit()
# main function
def main(dbname, user, password, hummus):
conn = sqlite3.connect('Practice.db')
c = conn.cursor()
if hummus == "newuser":
print "Creating user {user}".format(user=user)
createuser(user, password, dbname)
else:
print "Not running"
pass
# Create table
# c.execute('''CREATE TABLE login
# (Username text, Password text )''')
# Insert a row of data
# c.execute("INSERT INTO username VALUES
# ('raw_input()','BUY','RHAT',100,35.14)")
# creates new user in table 'username'
# creates new user in table 'username'
def createuser(user,password,dbname):
sql_insert = "INSERT INTO login VALUES
('{user}','{password}')".format(user=user, password=password)
print sql_insert
conn = sqlite3.connect(dbname)
c = conn.cursor()
c.execute(sql_insert)
# 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()
if __name__ == '__main__':
#setup()
#exit()
if DEBUG:
db = 'Practice.db'
user = 'tim'
password = 'massimo'
pass_check = True
main(db, user, password, 'newuser')
else:
db = 'Practice.db'
user = raw_input('Enter username ')
password = raw_input('Enter password ')
pass_check = raw_input('verify password ')
if not password == pass_check:
print ('Passwords do not match. ')
main(db, user, password, 'newuser')
#conn = sqlite3.connect(dbname)
#conn.commit()
#elif perform == ("login",'Login'):
#main(db, user, password, action)
编辑
因为我是从本地主机运行的,所以问题是我在尝试添加数据库的同时打开了数据库。 SQLite的DB Browser阻止了这一点。