MySQLdb Python execute not returning Table - python

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.

Related

How to load csv into an empty SQL table, using python?

So, I have this empty table which I created (see code below) and I need to load it with data from a csv file, using python-sql connection. As I do this, need to replace the html codes and change to correct datatypes (clean the file) and finally load it into this empty sql table.
This is the code I wrote but, without any success...when I check the table in SQL it just returns an empty table:
Python code:
import csv
with open ('UFOGB_Observations.csv', 'r') as UFO_Obsr:
## Write to the csv file, to clean it and change the html codes:
with open ('UFO_Observations.csv', 'w') as UFO_Obsw:
for line in UFO_Obsr:
line = line.replace('&#44', ',')
line = line.replace('&#39', "'")
line = line.replace('&#33', '!')
line = line.replace('&', '&')
UFO_Obsw.write(line)
##To Connect Python to SQL:
import pyodbc
print('Connecting...')
conn = pyodbc.connect('Trusted_Connection=yes', driver = '{ODBC Driver 13 for SQL Server}', server = '.\SQLEXPRESS', database = 'QA_DATA_ANALYSIS')
print('Connected')
cursor = conn.cursor()
print('cursor established')
cursor.execute('''DROP TABLE IF EXISTS UFO_GB_1;
CREATE TABLE UFO_GB_1 (Index_No VARCHAR(10) NOT NULL, date_time VARCHAR(15) NULL, city_or_state VARCHAR(50) NULL,
country_code VARCHAR(50) NULL, shape VARCHAR (200) NULL, duration VARCHAR(50) NULL,
date_posted VARCHAR(15) NULL, comments VARCHAR(700) NULL);
''')
print('Commands succesfully completed')
#To insert that csv into the table:
cursor.execute('''BULK INSERT QA_DATA_ANALYSIS.dbo.UFO_GB_1
FROM 'F:\GSS\QA_DATA_ANALYSIS_LEVEL_4\MODULE_2\Challenge_2\TASK_2\UFO_Observations.csv'
WITH ( fieldterminator = '', rowterminator = '\n')''')
conn.commit()
conn.close()
I was expecting to see a table with all 1900+ rows, when I type SELECT * FROM table, with correct data types (i.e. date_time and date_posted columns as timestamp)
(Apologies in advance. New here so not allowed to comment.)
1) Why are you creating the table each time? Is this meant to be a temporary table?
2) What do you get as a response to your query?
3) What happens when you break the task down into parts?
Does the code create the table?
If the table already exists and you run just the insert data code does it work? When you import the csv and then write back to the same file does that produce the result you are looking for or crash? What if you wrote to a different file and imported that?
you are writing your queries like how you would in SQL, but you need to re-write them in python. Python needs to understand the query as a python string, then it can parse it into sql. IE don't wrap the statement with '''.
This is not tested, but try something like this:
bulk_load_sql = """
BULK INSERT QA_DATA_ANALYSIS.dbo.UFO_GB_1
FROM 'F:\GSS\QA_DATA_ANALYSIS_LEVEL_4\MODULE_2\Challenge_2\TASK_2\UFO_Observations.csv'
WITH ( fieldterminator = '', rowterminator = '\n')
"""
cursor.execute(bulk_load_sql)
This uses a docstring to put the sql on multiple lines, but you may want to use a regular string.
Here is an answer that goes over formatting your query for pyodbc
https://stackoverflow.com/a/43855693/4788717

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

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.

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.

sqlite3 simple query in python hangs

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.

DB-API with Python

I'm trying to insert some data into a local MySQL database by using MySQL Connector/Python -- apparently the only way to integrate MySQL into Python 3 without breaking out the C Compiler.
I tried all the examples that come with the package; Those who execute can enter data just fine. Unfortunately my attempts to write anything into my tables fail.
Here is my code:
import mysql.connector
def main(config):
db = mysql.connector.Connect(**config)
cursor = db.cursor()
stmt_drop = "DROP TABLE IF EXISTS urls"
cursor.execute(stmt_drop)
stmt_create = """
CREATE TABLE urls (
id TINYINT UNSIGNED NOT NULL AUTO_INCREMENT,
str VARCHAR(50) DEFAULT '' NOT NULL,
PRIMARY KEY (id)
) CHARACTER SET 'utf8'"""
cursor.execute(stmt_create)
cursor.execute ("""
INSERT INTO urls (str)
VALUES
('reptile'),
('amphibian'),
('fish'),
('mammal')
""")
print("Number of rows inserted: %d" % cursor.rowcount)
db.close()
if __name__ == '__main__':
import config
config = config.Config.dbinfo().copy()
main(config)
OUTPUT:
Number of rows inserted: 4
I orientate my code strictly on what was given to me in the examples and can't, for the life of mine, figure out what the problem is. What am I doing wrong here?
Fetching table data with the script works just fine so I am not worried about the configuration files. I'm root on the database so rights shouldn't be a problem either.
You need to add a db.commit() to commit your changes before you db.close()!

Categories