i'm trying to make a simple query in python and sqlite3:
#!/usr/bin/env python
# -*- coding: utf-8; -*-
import sqlite3
db = sqlite3.connect('test.db')
query = """CREATE TABLE `home` (
`id` int(11) not null auto_increment,
`full_name` char(255) not null,
`display_name` char(255),
`ip_address` char(255) not null,
`user` char(255) not null,
PRIMARY KEY (`id`)
);"""
db.execute(query)
db.commit()
db.close()
But when i run the script, nothing happens; i mean: a file called test.db is created in the directory, but after that the shell remain there without return anything (even the prompt) and i need to kill the script with kill -9
Any help?
I don't know why your script apparently hangs, but there are SQL syntax errors in your query:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.OperationalError: near "auto_increment": syntax error
SQLite only have a AUTOINCREMENT keyword, and that only is supported after the PRIMARY KEY constraint; you'll have to move your PRIMARY KEY line from the bottom to the id column. You'll have to use the proper INTEGER type as well, instead of int.
SQLite also ignores column sizes, you may as well drop those.
The following query works:
query = """CREATE TABLE `home` (
`id` integer primary key autoincrement,
`full_name` char(255) not null,
`display_name` char(255),
`ip_address` char(255) not null,
`user` char(255) not null
);"""
where I left in the column sizes for the char columns, but you may as well make those TEXT (without a size) and be done with it.
Related
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
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.
Recently,I've got a project that I need to save variable values into a database.
I want to program a code like this:
If I want to input username="Jonh", gender="Male", age=23, password="123456789", id="11111111"
then the code looks like:
#!/usr/bin/python
# -*- coding: UTF-8 -*-
import MySQLdb
import sys
db = MySQLdb.connect("140.120.31.124","usr1","606","testdb",port=3306 )
cursor = db.cursor()
urn="Jonh"
gdr="male"
agex="23"
psd="123456789"
idx="11111111"
sql = """INSERT INTO table1(username,gender,age,password,id) VALUES(%s,%s,%s,%s,%s)""" %(urn,gdr,agex,psd,idx)
#sql="""INSERT INTO table1(username,gender,age,password,id) VALUES("Jonh","male","23","123456789","11111111")"""
try:
cursor.execute(sql)
db.commit()
except Exception as inst:
db.rollback()
print inst
db.close()
The structure of database I set is:
username varchar(50) utf8_unicode_ci
gender varchar(50) utf8_unicode_ci
age int(2)
password varchar(20) utf8_unicode_ci
id varchar(8) utf8_unicode_ci
But, it always shows the error --> (1054, "Unknown column 'Jonh' in 'field list'")
Does anyone can help me to solve this problem? Thanks a lot.
PS. The code I refer is https://www.packtpub.com/mapt/book/big_data_and_business_intelligence/9781849510189/3/ch03lvl1sec24/using-user-defined-variables
You must not use string substitution. Use parameters.
sql = """INSERT INTO table1(username,gender,age,password,id) VALUES(%s,%s,%s,%s,%s)"""
cursor.execute(sql, (urn,gdr,agex,psd,idx))
That book appears to be recommending unsafe practices. You should stop reading it immediately.
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
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.