Can't insert into table if table already exists - python

So I'm trying to insert a class variable into a mysql database and I'm having problems.
mysql.connector.errors.ProgrammingError: 1050 (42S01): Table 'ordertable' already exists
This message pops up when i try to insert values into a table even when i know the table exists.
order.py
import tkinter
from tkinter import *
import connection
import mysql.connector
class MenuWindow():
def init(self):
db = mysql.connector.connect(host="localhost",user="root", passwd="", database = "pizza")
mycursor =db.cursor()
orderData = [(None, self.varTotal.get())]
for element in orderData:
mycursor.execute("INSERT INTO ordertable (orderid,total) VALUES (?,?)", element)
db.commit()
The table is created in this file
connection.py
def get_connection():
db = mysql.connector.connect(host="localhost",user="root", passwd="", database = "pizza")
mycursor = db.cursor()
mycursor.execute("CREATE TABLE orderTable (orderid INT AUTO_INCREMENT PRIMARY KEY, total INT)")

The error message does not complain about the insert. It complains about the create, because connection.py attempts to create the table when it already exists.
You only need to create the table exactly once, you do not need to create it whenever you reconnect.
Remove the line of
mycursor.execute("CREATE TABLE orderTable (orderid INT AUTO_INCREMENT PRIMARY KEY, total INT)")

Related

Copying a table from an in-memory database to a on-disk database in sqlite3

I want to copy one specific table from an in-memory sqlite database, using Python (3.9). Looking in the documentation, I should be able to simply attach the in-memory database to the file database, however while attaching throws no error, when I try to copy the table over, it results in the following error:
Traceback (most recent call last):
File "[insert python file URL here]", line 21, in
file_cursor.execute(f'CREATE TABLE "{table_name}" AS SELECT * FROM db."{table_name}";')
sqlite3.OperationalError: no such table: db.table_name
So, even though it is attaching the database just fine (or at least a memory database), it appears not to be able to find the tables within that database. What can I do?
test code:
import sqlite3
#memory database
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
#file database
file_conn= sqlite3.connect(r"C:\...\testfile.sqlite") #insert your own URL here
file_cursor = file_conn.cursor()
table_name = "table_name"
#Create table in memory db
cursor.execute(f"CREATE TABLE {table_name} (id INTEGER, Value INTEGER, Value2 INTEGER, Category INTEGER);")
conn.commit()
cursor.execute(f"INSERT INTO {table_name} (id, Value, Value2, Category) "
f"VALUES ('1', '20','20', '2'),"
f"('2', '30','30', '2'),"
f"('13', '17','17','1');")
conn.commit()
# copy table to file db
file_cursor.execute(f"ATTACH DATABASE ':memory:' AS db;")
file_conn.commit()
file_cursor.execute(f"CREATE TABLE '{table_name}' AS SELECT * FROM db.'{table_name}';")
file_conn.commit()
file_conn.close()
conn.close()
You don't need a separate connection to the file db.
Use the connection of the in-memory db to attach the file db:
import sqlite3
#connection to memory db
conn = sqlite3.connect(":memory:")
cursor = conn.cursor()
table_name = "table_name"
#Create table in memory db
cursor.execute(f"CREATE TABLE [{table_name}] (id INTEGER, Value INTEGER, Value2 INTEGER, Category INTEGER);")
cursor.execute(f"INSERT INTO [{table_name}] (id, Value, Value2, Category) "
f"VALUES ('1', '20','20', '2'),"
f"('2', '30','30', '2'),"
f"('13', '17','17','1');")
conn.commit()
# copy table to file db
cursor.execute(r"ATTACH DATABASE 'C:\...\testfile.sqlite' AS db;")
cursor.execute(f"CREATE TABLE db.[{table_name}] AS SELECT * FROM [{table_name}];")
conn.commit()
conn.close()

Insert Variables With Python And Tkinter, Into Sqlite3 Table

Sorry this is my first post.
I try to insert into sqlite3 table datas that i get using tkinter entries (python)
but i always obtain empty fields in the table.my code:
import sqlite3
from tkinter import *
def data_entry():
CDB.execute('insert into COSTUMERS (NAME,CODE)values(?,?)', (NAME_E,CODE_E))
DB.commit()
CDB.close()
DB.close()
X=Tk()
NAME=StringVar()
CODE=StringVar()
DB=sqlite3.connect('DB.db')
CDB=DB.cursor()
CDB.execute('''create table if not exists COSTUMERS
(ID integer primary key autoincrement,
NAME text(20), CODE text(10))''')
NAME_E=Entry(X,textvariable=NAME).pack()
CODE_E=Entry(X,textvariable=CODE).pack()
SAVE=Button(X,text='SAVE',command=data_entry).pack()
X.mainloop()
I think you should refactoring your code.
First of all use a naming convention on sql commands, that is, the uppercase
commands and the rest in lowercase.
This also for what concerns the code, see pep 8
I modified your script, in sqlite you don’t need to declare an autoincrement field
if you declare it as primary key.
I did not close the cursor and the database to insert more records as you can see
from the attached images.
And you don’t even need to declare Entry if you use textvariable, you can use
these directly to pass the values.
#!/usr/bin/python3
import tkinter as tk
import sqlite3 as lite
def data_entry():
sql = "INSERT INTO customers (customer,code)VALUES(?,?)"
args = (customer.get(),code.get())
print(sql, args)
cur.execute(sql, args)
dbms.commit()
sql = "SELECT * FROM customers"
cur.execute(sql)
rs = cur.fetchall()
for i in rs:
print(i)
#cur.close()
#dbms.close()
tk.X=tk.Tk()
customer = tk.StringVar()
code = tk.StringVar()
dbms = lite.connect('DB.db')
cur = dbms.cursor()
sql = "CREATE TABLE IF NOT EXISTS customers (customer_id INTEGER PRIMARY KEY, customer TEXT, code TEXT);"
cur.execute(sql)
tk.Entry(tk.X,textvariable=customer).pack()
tk.Entry(tk.X,textvariable=code).pack()
tk.Button(tk.X, text="Save", command=data_entry).pack()
tk.X.mainloop()

Python SQLite3: Cannot Create Table with Variable in Query

I have the following code to create a table if it does not already exist in a database.
TABLE_NAME = 'Test'
sql = sqlite3.connect('DATABASE.db')
cur = sql.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS ? (id TEXT)', [TABLE_NAME])
sql.commit()
But I keep getting sqlite3.OperationalError: near "?": syntax error
I have other code such as cur.execute('INSERT * INTO database VALUES(?,?)', [var1, var2]) that works fine.
That is correct, parameters cannot be used to substitute for database identifiers, only for values. You will have to build the SQL command, with the table name specified, as a string.
The following code creates the table
import sqlite3
sql = sqlite3.connect('DATABASE.db')
cur = sql.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS Test (id TEXT)')
sql.commit()

How do I insert data into table?

I have created table using this create command as:
CREATE TABLE test_table(id INT PRIMARY KEY,name
VARCHAR(50),price INT)
i want to insert into this table wherein values are stored already in variable
bookdb=# name = 'algorithms'
bookdb-# price = 500
bookdb-# INSERT INTO test_table VALUES(1,'name',price);
I get the following error:
ERROR: syntax error at or near "name"
LINE 1: name = 'algorithms'
Can anyone point out the mistake and propose solution for the above?
Thanks in advance
Edit:
import psycopg2
import file_content
try:
conn = psycopg2.connect(database='bookdb',user='v22')
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS book_details")
cur.execute("CREATE TABLE book_details(id INT PRIMARY KEY,name VARCHAR(50),price INT)")
cur.execute("INSERT INTO book_details VALUES(1,'name',price)")
conn.commit()
except:
print "unable to connect to db"
I have used the above code to insert values into table,variables name and price containing the values to be inserted into table are available in file_content python file and i have imported that file.The normal INSERT statement takes values manually but i want my code to take values which are stored in variables.
SQL does not support the concept of variables.
To use variables, you must use a programming language, such as Java, C, Xojo. One such language is PL/pgSQL, which you can think of as a superset of SQL. PL/PgSQL is often bundled as a part of Postgres installers, but not always.
I suggest you read some basic tutorials on SQL.
See this similar question: How do you use script variables in PostgreSQL?
don't have postgres installed here, but you can try this
import psycopg2
import file_content
try:
conn = psycopg2.connect(database='bookdb',user='v22')
cur = conn.cursor()
cur.execute("DROP TABLE IF EXISTS book_details")
cur.execute("CREATE TABLE book_details(id INT PRIMARY KEY,name VARCHAR(50),price INT)")
cur.execute("INSERT INTO book_details VALUES(1, '%s', %s)" % (name, price))
conn.commit()
except:
print "unable to connect to db"
If you are using PSQL console:
\set name 'algo'
\set price 10
insert into test_table values (1,':name',:price)
\g

Create MySQLdb database using Python script

I'm having troubles with creating a database and tables. The database needs to be created within a Python script.
#connect method has 4 parameters:
#localhost (where mysql db is located),
#database user name,
#account password,
#database name
db1 = MS.connect(host="localhost",user="root",passwd="****",db="test")
returns
_mysql_exceptions.OperationalError: (1049, "Unknown database 'test'")
So clearly, the db1 needs to be created first, but how? I've tried CREATE before the connect() statement but get errors.
Once the database is created, how do I create tables?
Thanks,
Tom
Here is the syntax, this works, at least the first time around. The second time naturally returns that the db already exists. Now to figure out how to use the drop command properly.
db = MS.connect(host="localhost",user="root",passwd="****")
db1 = db.cursor()
db1.execute('CREATE DATABASE test1')
So this works great the first time through. The second time through provides a warning "db already exists". How to deal with this? The following is how I think it should work, but doesn't. OR should it be an if statement, looking for if it already exists, do not populate?
import warnings
warnings.filterwarnings("ignore", "test1")
Use CREATE DATABASE to create the database:
db1 = MS.connect(host="localhost",user="root",passwd="****")
cursor = db1.cursor()
sql = 'CREATE DATABASE mydata'
cursor.execute(sql)
Use CREATE TABLE to create the table:
sql = '''CREATE TABLE foo (
bar VARCHAR(50) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
'''
cursor.execute(sql)
There are a lot of options when creating a table. If you are not sure what the right SQL should be, it may help to use a graphical tool like phpmyadmin to create a table, and then use SHOW CREATE TABLE to discover what SQL is needed to create it:
mysql> show create table foo \G
*************************** 1. row ***************************
Table: foo
Create Table: CREATE TABLE `foo` (
`bar` varchar(50) DEFAULT NULL
) ENGINE=MyISAM DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
phpmyadmin can also show you what SQL it used to perform all sorts of operations. This can be a convenient way to learn some basic SQL.
Once you've experimented with this, then you can write the SQL in Python.
I think the solution is a lot easier, use "if not":
sql = "CREATE DATABASE IF NOT EXISTS test1"
db1.execute(sql)
import MySQLdb
# Open database connection ( If database is not created don't give dbname)
db = MySQLdb.connect("localhost","yourusername","yourpassword","yourdbname" )
# prepare a cursor object using cursor() method
cursor = db.cursor()
# For creating create db
# Below line is hide your warning
cursor.execute("SET sql_notes = 0; ")
# create db here....
cursor.execute("create database IF NOT EXISTS yourdbname")
# create table
cursor.execute("SET sql_notes = 0; ")
cursor.execute("create table IF NOT EXISTS test (email varchar(70),pwd varchar(20));")
cursor.execute("SET sql_notes = 1; ")
#insert data
cursor.execute("insert into test (email,pwd) values('test#gmail.com','test')")
# Commit your changes in the database
db.commit()
# disconnect from server
db.close()
#OUTPUT
mysql> select * from test;
+-----------------+--------+
| email | pwd |
+-----------------+--------+
| test#gmail.com | test |
+-----------------+--------+

Categories

Resources