I am working on a sqlite database python based project , to keep the code neat and clean I am passing the values of my form fields along with database name and table name to another python file , whose work is to handle only database queries , I have framed the query everthing is working fine but I am getting error in execute() function.
import sqlite3
from PyQt5.QtWidgets import QMessageBox
class Db:
def init(self):
pass
<code>def DbConnection(self ,databaseName, databaseTable, tableColumnValueList):
try:
print (databaseName, databaseTable, tableColumnValueList)
connection = sqlite3.connect (databaseName)
# Create a cursor object
cursor = connection.cursor ()
# Query to get column names
cursor.execute (f"PRAGMA table_info({databaseTable});")
# Fetch all results
columns_info = cursor.fetchall ()
# Extract and print column names
column_names = [info[1] for info in columns_info]
print ("Column names:", column_names)
w = "?," * (len (column_names) - 1)
w = w[:-1] + ""
print (w)
query = "'INSERT into" + " " + databaseTable + " ("
# Appending columns name in a query string
for i in range (1, len (column_names)):
query += column_names[i] + ","
query = query[:-1] + ") VALUES(" + w + ")',("
# Appending columns values in a query string
for i in range (len (tableColumnValueList)):
query += str(tableColumnValueList[i]) + ","
query = query[:-1] + ")"
print(query)
cursor.execute(query)
connection.commit ()
cursor.close()
# Close the connection
connection.close ()
except Exception as e:
#QMessageBox.about (self, "Exception", 'Error: "{}"'.format (e))
print(e)
</code>
<code>def DbConnection(self ,databaseName, databaseTable, tableColumnValueList):
try:
print (databaseName, databaseTable, tableColumnValueList)
connection = sqlite3.connect (databaseName)
# Create a cursor object
cursor = connection.cursor ()
# Query to get column names
cursor.execute (f"PRAGMA table_info({databaseTable});")
# Fetch all results
columns_info = cursor.fetchall ()
# Extract and print column names
column_names = [info[1] for info in columns_info]
print ("Column names:", column_names)
w = "?," * (len (column_names) - 1)
w = w[:-1] + ""
print (w)
query = "'INSERT into" + " " + databaseTable + " ("
# Appending columns name in a query string
for i in range (1, len (column_names)):
query += column_names[i] + ","
query = query[:-1] + ") VALUES(" + w + ")',("
# Appending columns values in a query string
for i in range (len (tableColumnValueList)):
query += str(tableColumnValueList[i]) + ","
query = query[:-1] + ")"
print(query)
cursor.execute(query)
connection.commit ()
cursor.close()
# Close the connection
connection.close ()
except Exception as e:
#QMessageBox.about (self, "Exception", 'Error: "{}"'.format (e))
print(e)
</code>
def DbConnection(self ,databaseName, databaseTable, tableColumnValueList):
try:
print (databaseName, databaseTable, tableColumnValueList)
connection = sqlite3.connect (databaseName)
# Create a cursor object
cursor = connection.cursor ()
# Query to get column names
cursor.execute (f"PRAGMA table_info({databaseTable});")
# Fetch all results
columns_info = cursor.fetchall ()
# Extract and print column names
column_names = [info[1] for info in columns_info]
print ("Column names:", column_names)
w = "?," * (len (column_names) - 1)
w = w[:-1] + ""
print (w)
query = "'INSERT into" + " " + databaseTable + " ("
# Appending columns name in a query string
for i in range (1, len (column_names)):
query += column_names[i] + ","
query = query[:-1] + ") VALUES(" + w + ")',("
# Appending columns values in a query string
for i in range (len (tableColumnValueList)):
query += str(tableColumnValueList[i]) + ","
query = query[:-1] + ")"
print(query)
cursor.execute(query)
connection.commit ()
cursor.close()
# Close the connection
connection.close ()
except Exception as e:
#QMessageBox.about (self, "Exception", 'Error: "{}"'.format (e))
print(e)