'create table if not exists' query still giving warning if table exists - python

This is my python code:
cursor = conn.cursor ()
cursor.execute("CREATE DATABASE IF NOT EXISTS my_db")
cursor.execute("USE my_db")
table = """CREATE TABLE IF NOT EXISTS `table1` (
`id` INT NOT NULL,
`rank` INT NULL,
PRIMARY KEY (`id`)
)"""
cursor.execute(table)
It ran perfectly the first time when the db and table didn't exist. I ran it a second time to check if the IF NOT EXIST clause worked correctly. But it didn't! I got the following warnings:
script.py:67: Warning: Can't create database 'my_db'; database exists
cursor.execute("CREATE DATABASE IF NOT EXISTS my_db")
script.py:79: Warning: Table 'table1' already exists
cursor.execute(table)
Did I format my query incorrectly somehow? It matches everything I'm finding online as far as I can tell.

Related

Conditionally Create Temp Table in SQL from Python

In a large set of queries, I'm trying to create a temp table in SQL, if it doesn't already exist. Obviously, you could remove the 2nd CREATE TABLE statement. However, the queries I'm building are dynamic and may, or may not, have the 1st CREATE TABLE statement present.
I can get the following sample/test query to work in Microsoft SQL Server Management Studio. It was created with help from this SO question/answer
SET NOCOUNT ON;
DROP TABLE IF EXISTS #temp_sample;
CREATE TABLE #temp_sample (
id VARCHAR(15) NOT NULL,
datetime DATETIME,
location VARCHAR(255)
);
GO
INSERT INTO #temp_sample (id, datetime, location)
VALUES ('ABC', '2021-06-04 15:52:44', 'PENNSYLVANIA'),('123', '2021-06-04 15:52:49', 'PENNSYLVANIA');
IF (OBJECT_ID('tempdb..#temp_sample') IS NULL)
BEGIN
CREATE TABLE #temp_sample (
id VARCHAR(15) NOT NULL,
datetime DATETIME,
location VARCHAR(255)
);
END
ELSE
PRINT '#temp_sample already exists... skipping'
GO
SELECT * FROM #temp_sample
WHEN I run the following code in the same database, but using pandas.io.sql.read_sql and pypyodbc I get the accompanying traceback:
import pypyodbc
import pandas.io.sql as psql
connection_string = 'DSN=dsn_name;UID=username;PWD=password;app=app_name;'
cnxn = pypyodbc.connect(connection_string)
temp_db_query = '''
SET NOCOUNT ON;
DROP TABLE IF EXISTS #temp_sample;
CREATE TABLE #temp_sample (
id VARCHAR(15) NOT NULL,
datetime DATETIME,
location VARCHAR(255)
);
GO
INSERT INTO #temp_sample (id, datetime, location)
VALUES ('ABC', '2021-06-04 15:52:44', 'PENNSYLVANIA'),('123', '2021-06-04 15:52:49', 'PENNSYLVANIA');
IF (OBJECT_ID('tempdb..#temp_sample') IS NULL)
BEGIN
CREATE TABLE #temp_sample (
id VARCHAR(15) NOT NULL,
datetime DATETIME,
location VARCHAR(255)
);
END
ELSE
PRINT '#temp_sample already exists... skipping'
GO
SELECT * FROM #temp_sample
'''
df = psql.read_sql(temp_db_query, cnxn)
cnxn.close()
Traceback (most recent call last):
File "/Users/user/miniconda3/envs/myenv/lib/python3.6/site-packages/pandas/io/sql.py", line 1595, in execute
cur.execute(*args)
File "/Users/user/miniconda3/envs/myenv/lib/python3.6/site-packages/pypyodbc.py", line 1626, in execute
self.execdirect(query_string)
File "/Users/user/miniconda3/envs/myenv/lib/python3.6/site-packages/pypyodbc.py", line 1652, in execdirect
check_success(self, ret)
File "/Users/user/miniconda3/envs/myenv/lib/python3.6/site-packages/pypyodbc.py", line 1007, in check_success
ctrl_err(SQL_HANDLE_STMT, ODBC_obj.stmt_h, ret, ODBC_obj.ansi)
File "/Users/user/miniconda3/envs/myenv/lib/python3.6/site-packages/pypyodbc.py", line 975, in ctrl_err
raise ProgrammingError(state,err_text)
pypyodbc.ProgrammingError: ('42S01', "[42S01] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]There is already an object named '#temp_sample' in the database.")
Can someone help me get the query to work using pandas.io.sql.read_sql? I'm open to switching to another odbc package like pyodbc, turbodbc, etc.
======= UPDATE ========
Based on one of the comments, I tried changing the logic in the IF statement because it appears that, when using ODBC, it's getting flagged as TRUE. This version also works in MSSMS, but gives me the same error in Python. Is there another version that works?
IF EXISTS (SELECT * FROM tempdb.sys.tables WHERE name LIKE '#temp_sample%')
PRINT '#temp_sample already exists... skipping'
ELSE
BEGIN
CREATE TABLE #temp_sample (
id VARCHAR(15) NOT NULL,
datetime DATETIME,
location VARCHAR(255)
);
END
GO
This is a batch compilation error. When you remove the GO, which you must to to get this to compile, then there are two CREATE TABLE statements for the same temp table, which won't parse and compile. EG this batch generates the same error:
CREATE TABLE #temp_sample (id int)
if 1=0
begin
CREATE TABLE #temp_sample (id int)
end
To fix, just remove the second CREATE TABLE, as it's unnecessary anyway. EG
SET NOCOUNT ON;
DROP TABLE IF EXISTS #temp_sample;
CREATE TABLE #temp_sample (
id VARCHAR(15) NOT NULL,
datetime DATETIME,
location VARCHAR(255)
);
INSERT INTO #temp_sample (id, datetime, location)
VALUES ('ABC', '2021-06-04 15:52:44', 'PENNSYLVANIA'),('123', '2021-06-04 15:52:49', 'PENNSYLVANIA');
SELECT * FROM #temp_sample

How to create a table in psql database using python?

I am running this script and want to create a table by passing values in psql query using variables. So, that I can create multiple table in one go. But this cur.execute("CREATE TABLE IF NOT EXISTS(%s, %s)",[table_name, comp_schema]) line is throwing error. How can I write this query to create a table with the given schema?
import psycopg2
conn = psycopg2.connect(database="review_check", user = "xxx", password = "xxx",)
cur = conn.cursor()
print ("Opened database successfully")
comp_schema = """
as_of_date DATE PRIMARY KEY NOT NULL,
verified_reviews INTEGER,
lsa_total_reviews INTEGER
"""
table_name = 'comp_first'
cur.execute("CREATE TABLE IF NOT EXISTS(%s, %s)",[table_name, comp_schema])
conn.commit()
conn.close()
Use the psycopg2.sql module to compose a query dynamically. see https://www.psycopg.org/docs/sql.html.
There's a couple of errors here, in both the SQL syntax and the Python:
cur.execute("CREATE TABLE IF NOT EXISTS(%s, %s)",[table_name, comp_schema])
should be
cur.execute("CREATE TABLE IF NOT EXISTS %s (%s)"%(table_name, comp_schema))
It might be easier during development to build the query in a separate variable first, then print it to see if it looks right:
test = "CREATE TABLE IF NOT EXISTS %s (%s)"%(table_name, comp_schema)
print(test)
>>CREATE TABLE IF NOT EXISTS comp_first (
as_of_date DATE PRIMARY KEY NOT NULL,
verified_reviews INTEGER,
lsa_total_reviews INTEGER
)

Not able to insert into mysql database

I have just started using MySQLdb in python. I am able to create table but when I try to insert, no rows are inserted into the table and its shows that table is still empty.
import MySQLdb
db = MySQLdb.connect("localhost","root","shivam","test")
cursor = db.cursor()
s = "DROP TABLE IF EXISTS batting"
cursor.execute(s)
s = """create table batting (
name varchar(50) primary key,
matches integer(5),
innings integer(5),
runs integer(5),
highest integer(3),
strikerate integer(3),
hundreds integer(3),
fifties integer(3)
)"""
cursor.execute(s)
s = """insert into batting(name) values(
"shivam")"""
cursor.execute(s)
db.close()
Where I could be going wrong?
You forgot to commit your connection. Simply add:
cursor.execute(s)
db.commit()
Have a look at this. It explains why you need to commit

MySQLdb Python execute not returning Table

I've been trying to use python's MySQLdb to execute SQL on a MySQL Database from SSH on my webhost. This program i wrote (on a mac) should print a table, but it doesn't.
Here's my code:
import MySQLdb
db = MySQLdb.connect("my host","my username","my password","my database")
cursor = db.cursor()
cursor.execute('''
DROP TABLE IF EXISTS news;
CREATE TABLE news
(
id int unsigned NOT NULL auto_increment,
headline varchar(250) NOT NULL,
summary varchar(5000) NOT NULL,
date varchar(50) NOT NULL,
link varchar(2500) NOT NULL,
imagelink varchar(2500) NOT NULL,
category varchar(50) NOT NULL,
PRIMARY KEY (id)
);
insert into news (headline, summary, date, link, imagelink, category)
values ("The worlds awesomest news source.", "Released by So-and-so , this will be the greatest thing ever.", "Aug. 11", "http://www.google.com/", "http://www.example.com/", "World");
SELECT summary FROM news WHERE id = 1;
''')
results = cursor.fetchall()
print results
... and the output in Mac Terminal is:
()
Please tell me what the problem is, and how to fix it. Thank you!
-CJ
Please separate each SQL commands into separate calls to execute().
Combining multiple SQL commands in such a fashion may not work, and also separating out into multiple execute() commands make debugging easier.

Error 1065 'Query was Empty' In python script

Error 1065 'Query was Empty' In python script
I have kept all the create table SQL querys in a text file. Using readLines i am trying to execute the sql commands as per the code mentioned.
file=open("TABLES.txt","r")
for sql in file.readlines():
self.cursor.execute(sql)
But I am getting Error 1065 ' Query was empty'. More Importantly the tables are being created in the database. The text file is like this
CREATE TABLE TUserDetails (FirstName VarChar(50) NOT NULL, LastName VarChar(50) NOT NULL, EmailId VarChar(50) NOT NULL,Type VarChar(50) NOT NULL,Department VarChar(50) NOT NULL,NoOfIncorrectAttempt Integer NOT NULL,Deleted Bit NOT NULL,UserID VarChar(50) NOT NULL,CONSTRAINT TUserDetailsPK PRIMARY KEY CLUSTERED ( UserID ))
CREATE TABLE TRequests(RequestID VarChar(50) NOT NULL,UserID VarChar(50) NOT NULL,Status SmallInt NOT NULL,TimeOfRequest Timestamp NOT NULL,Deleted Bit NOT NULL,Priority Integer NOT NULL,CONSTRAINT TRequests_PK PRIMARY KEY CLUSTERED ( RequestID ))
I checked running each sql query individually, and it is working file. Now although the tables are being created in the database but i am getting error 1065 as mentioned above
The error message is very clear: The query is empty. You're reading every line and executing it as a SQL query, and your file has a blank line between the CREATE TABLE statements.

Categories