не удалось получить базу данных SQLite в python - PullRequest
0 голосов
/ 29 мая 2020

1) Я новичок в python и использую базу данных SQLITE. При настройке соединения я не могу получить базу данных (которую я уже сделал в SQLite и таблицах)

2) При выполнении запроса на вставку я получаю следующую ошибку: «sqlite3.OperationalError: нет такой таблицы: MemberDetails "

ЗДЕСЬ КОД:

import sqlite3
my_cursor = ""
conn = ""


# setting the connection with database
# def con_database_connection():
# try:
globals()
conn = sqlite3.connect('CareForNation.db')
# new_con = sqlite3.connect('CareForNation.db')
print("connection established successfully")
my_cursor = conn.cursor()
# except sqlite3 as error
# print("Connection failed")


# Login Admin Control
user_name = input("Enter user name")
user_password = input("Enter Password")
if user_name == "123" and user_password == "123":
    print("Login successful")
else:
    print("Invalid user name or password")


# Inserting member information
# con_database_connection()
mid = input("Enter Member ID")
member_name = input("Enter member name")
member_phone_number = input("enter phone number")
member_address = input("Enter address")

insert_data = my_cursor.execute("insert into MemberDetails(Member_ID, Member_Name, Phone_Number, address) "
                                "values('mid', 'member_name', 'member_phone_number', member_address)")

# abc = conn.executescript("select * from MemberDetails")

print("New data entered to Member details module")
conn.close()

Ответы [ 2 ]

0 голосов
/ 29 мая 2020

Сначала создайте таблицу:

my_cursor.execute("""CREATE TABLE MemberDetails(
            mid text,
            member_name text,
            member_phone_number text,
            member_address text
            )""")

Затем замените

insert_data = my_cursor.execute("insert into MemberDetails(Member_ID, Member_Name, Phone_Number, address)"
                                "values('mid', 'member_name', 'member_phone_number', member_address)"
                                )

на:

with conn:
    my_cursor.execute("INSERT INTO MemberDetails VALUES (:mid, :member_name, :member_phone_number, :member_address)",
                      {'mid': mid, 'member_name': member_name, 'member_phone_number': member_phone_number, 'member_address': member_address})
0 голосов
/ 29 мая 2020

[Обновлено]

Я внес некоторые изменения в ваш код, теперь он работает отлично!

import sqlite3
my_cursor = ""
conn = ""


# setting the connection with database
# def con_database_connection():
# try:
globals()
conn = sqlite3.connect('CareForNation.db')
# new_con = sqlite3.connect('CareForNation.db')
print("connection established successfully")
my_cursor = conn.cursor()
# except sqlite3 as error
# print("Connection failed")


# Login Admin Control
user_name = input("Enter user name")
user_password = input("Enter Password")
if user_name == "123" and user_password == "123":
    print("Login successful")
else:
    print("Invalid user name or password")


# Inserting member information
# con_database_connection()
mid = input("Enter Member ID")
member_name = input("Enter member name")
member_phone_number = input("enter phone number")
member_address = input("Enter address")

my_cursor.execute("create table if not exists MemberDetails(Member_ID, Member_Name, Phone_Number, address)")
insert_data = my_cursor.execute("insert into MemberDetails (Member_ID, Member_Name, Phone_Number, address) values (?, ?, ?, ?)", (mid, member_name, member_phone_number, member_address))
# abc = conn.executescript("select * from MemberDetails")

print("New data entered to Member details module")
conn.close()
~             
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...