0
所以我有两个.py文件,我试图从database_modules.py文件加载到main.py但是当我尝试运行该程序,我得到这个错误:功能从一个模块,说明他们没有定义
NameError: name 'create_table' is not defined
的main.py文件是在主目录而database_modules.py是在子目录(模块/ database_modules.py)
main.py:
import sqlite3
import datetime
from modules import database_modules
connection = sqlite3.connect('accounts.db')
cursor = connection.cursor()
create_table()
data_entry(input('Please input your name: '), hash(input('Please input your password: ')))
read_entry()
quit()
database_modules .P Y:
def create_table():
cursor.execute("CREATE TABLE IF NOT EXISTS accountDetails(name TEXT, password TEXT, dateCreation TEXT, accountID INT)")
def data_entry(name, password):
dateCreation = (datetime.datetime.now().strftime("%Y-%m-%d %H:%M"))
accountID = 1
cursor.execute("INSERT INTO accountDetails (name, password, dateCreation, accountID) VALUES (?, ?, ?, ?)",
(name, password, dateCreation, accountID))
connection.commit()
def read_entry():
cursor.execute("SELECT * FROM accountDetails")
data = cursor.fetchall()
print(data)
def quit():
cursor.close()
connection.close()
在此先感谢,因为我不确定如何获得的功能,文件工作得很好,直到我试图把我的代码到模块。
您必须使用'database_modules.create_table' –