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' –