sqlite3.OperationalError: no such column. But that's not a column - python

I seem to be getting a rather odd error. And, for the life of me I can't figure out what's wrong. But on a piece of SQLite code, I'm getting this error:
Traceback (most recent call last):
File "test.py", line 38, in <module>
populateTables()
File "test.py", line 20, in populateTables
curs.execute("SELECT * FROM tracks WHERE ISRC = " + line[8])
sqlite3.OperationalError: no such column: USTCZ0993316
The odd part is that USTCZ0993316 is a piece of data that I want to compare to. I don't know why it seems to think it is a column. Here is a much small version that gives the same issue.
import sqlite3
import csv
def tableSetup(name):
if(name=="tracks"):
curs.execute("CREATE TABLE tracks(id INT UNIQUE, name TINYTEXT, album_id INT, client_id INT, acr_record_num INT, ISRC TINYTEXT UNIQUE, track_length TINYTEXT, client_share FLOAT)")
def populateTables():
tracks_csv=csv.reader(open('tables/tracks.csv', 'rU'), delimiter=";", quotechar='"')
tracks_csv.next()
for line in tracks_csv:
curs.execute("SELECT * FROM tracks WHERE id = " + line[0])
if not curs.fetchall():
if "\"" in line[1]:
line[1]=line[1].replace("\"","'")
curs.execute("INSERT INTO tracks VALUES("+line[0]+",\""+line[1]+"\","+line[2]+","+line[3]+","+line[4]+",\""+line[5]+"\",\""+line[7]+"\","+line[12]+")")
override_csv=csv.reader(open('tables/artist_override.csv', 'rU'), delimiter=",", quotechar='"')
override_csv.next()
for line in override_csv:
curs.execute("SELECT * FROM tracks WHERE ISRC = " + line[8])
print curs.fetchone()
#Set required Table Names
tables = ["tracks"]
testOut=open('tables/testOut.txt','w')
conn = sqlite3.connect('tables/test.db')
curs = conn.cursor()
# Create table if they don't already exist
curs.execute("SELECT name FROM sqlite_master WHERE type='table'")
tableResults = curs.fetchall()
print
for table in tables:
if not any(table == result[0] for result in tableResults):
tableSetup(table)
populateTables()
conn.commit()
curs.close()

If it doesn't have quotes then it's a column or a number.
curs.execute("SELECT * FROM tracks WHERE ISRC = ?", (line[8],))
[Edit: "%s" isn't proper syntax. It should be "?". Also, if you're going to be condescending, at least be right about it.]

Related

SQLITE error while trying to parse every table data with python

I am trying to print the entire table from all the tables inside of a db that I have created.
When I try to print all columns for each table I get an error and it seems that the script tries to parse trough one of the inside elements (first column and first row data) and therefore bringing back an error by saying that there is no such a table.
Here is my code:
import sqlite3
conn = sqlite3.connect('amazon_pages.db')
c = conn.cursor()
all_tables_list = c.execute("select name from sqlite_master where type = 'table'")
for table in all_tables_list:
argument_execute = 'SELECT * FROM ' + table[0]
print(argument_execute)
c.execute(argument_execute)
This is the error that I get:
SELECT * FROM Apple_charger
Traceback (most recent call last):
SELECT * FROM B07JGMC714
File "/Users/Amato/PycharmProjects/Refine/Amazon_pages_sql_database_creator.py", line 36, in <module>
c.execute(argument_execute)
sqlite3.OperationalError: no such table: B07JGMC714
Process finished with exit code 1
How do I print all the entire tables from the db?
fetch the result first:
all_tables_list = c.execute("select name from sqlite_master where type = 'table'")
rows = all_tables_list.fetchall()
for row in rows:
print(row)

Operational error when inserting list of lists into sqlite table with executemany

I am creating a database and inserting a list of lists that are created from an excel sheet. The main list is created using openpyxl, that list is then split every 18 items into a list of lists. I would like to then insert all the items into the database. Not very familiar with SQL but after some research, I managed to put this together:
import sqlite3
from openpyxl import load_workbook
import os
filepath = os.path.expanduser("~\Desktop\\")
data1 = []
data=[]
wb1 = load_workbook(filename=filepath+"exportUL1.XLSX")
ws1 = wb1['Sheet1'] ###call the worksheet with the data
x = 0
for row in ws1.iter_rows():
#look for the correct value, "Q" and return all the data in that row to the data list
for cell in row:
if cell.value == 'Q':
data.append(x) #append the id numbers to the list
for cell in row:
data.append(str(cell.value)) #append the row data to the list
x += 1
data_lists = [data[x:x+18] for x in range(0, len(data),18)] #convert list to list of lists, split every 18 items
conn = sqlite3.connect('test.db')
print "Opened database successfully";
with conn:
cur = conn.cursor()
cur.execute('''CREATE TABLE QualityHold(
Id Int,StorageLocation TEXT, StorageType TEXT, StorageBin TEXT, StorageUnit TEXT,
Material TEXT, Plant TEXT, Batch TEXT, StockCategory TEXT, SpecialStock TEXT, SpecialStockNumber TEXT,
Duration TEXT, PutawayBlock TEXT, StockRemovalBlock TEXT, AvailableStock TEXT, StockforPutaway TEXT,
PickQuantity TEXT, TotalStock TEXT)''') #create the table with these headers
sql = '''INSERT INTO QualityHold (Id,StorageLocation,
StorageType, StorageBin, StorageUnit, Material,
Plant, Batch, StockCategory, SpecialStock,
SpecialStockNumber, Duration, PutawayBlock, StockRemovalBlock,
AvailableStock, StockforPutaway, PickQuantity, TotalStock)
VALUES
(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)'''#sql command
cur.executemany(sql,data_lists)###execute the sql command using the list of lists for the variables(LINE 38)
conn.close()
Once I run this I get the following error
Traceback (most recent call last):
File "C:\Users\ONP1LDY\eclipse-workspace\WOrk\QualityInspection.py", line 38, in <module>
cur.executemany(sql,data_lists)
sqlite3.OperationalError: near "%": syntax error
Any help with what would be causing this would be great!
I went a different route and replaced the executemany with the following:
for lst in data_lists:
var_string = ', '.join('?'*len(lst))
query_string = 'INSERT OR IGNORE INTO QualityHold VALUES (%s);' % var_string
cur.execute(query_string, lst)

Populate sqlite3 database from csv: Syntax error around '?'

Following a previous post concerning the population of a sqlite3 database from a csv file in python, I have used the code exactly as written but keep coming up with: Traceback (most recent call last):
File "Z:/KS4/Computer Science/OCR corsework/Task 1 Database/populate.py", line 10, in <module>
cursor.execute(query, data)
sqlite3.OperationalError: near "?": syntax error
This is the code:
import csv, sqlite3
connection = sqlite3.connect("TutorGroup.db")
with open ('studentsEmail-master.csv', 'r') as f:
r = csv.reader(f)
data = next(r)
query = 'insert into dbo.students ({0})'
query = query.format(','.join('?' * len(data)))
cursor = connection.cursor()
cursor.execute(query, data)
for data in reader:
cursor.execute(query, data)
cursor.commit()
https://www.sqlite.org/lang_insert.html
You need the word values in the query:
query = 'insert into dbo.students values ({0})'
# ^^^^^^

Why doesn't this work (sqlite, python)

I tried to do this in the interpreter and I can get it to work but inside my function it doesn't
What I'm trying to do:
cursor = dbconnect.cursor()
cursor.execute("""SELECT * FROM credits WHERE phone = ?""",(phone,))
data = cursor.fetchone()
firstname = data[1] #the db is set as firstname in position 1 after the id(primekey)
I'm actually extracting all the data using this method just with different variables
The Error I get when I do it inside the function:
firstname = data[1]
TypeError: 'NoneType' object is not subscriptable
As a note: I put a print statement after the data object to see what it was returning, in the interpreter it returns the tuple i'm searching for, inside the function it's returning
'None'
FULL CODE:
def FindByPhone(self,phone):
'''Find Credit by phone number ONLY'''
dbconnect = sqlite3.connect(self.dbname)
cursor = dbconnect.cursor()
cursor.execute("""SELECT * FROM credits WHERE phone = ?""",(phone,))
data = cursor.fetchone()
first = data[1]
last = data[2]
phone = data[3]
credit = data[4]
cid = data[0]
self.SetVariables(first,last,phone,credit,cid)
cursor.close()
dbconnect.close()
return
I think the problem is that your function doesn't check if there was a matching row in the database. you will get this error if no row is being returned:
#!/usr/bin/python
try:
import sqlite3
except:
from pysqlite2 import dbapi2 as sqlite3
#prepare testcase
db="/tmp/soverflow.sqlite"
dbconnect = sqlite3.connect(db)
c = dbconnect.cursor()
c.execute("""create table credits
(id int not null primary key, firstname varchar(50), phone varchar(30),amount int not null)""")
c.execute("""INSERT INTO credits (id,firstname,phone,amount) VALUES (1,'guybrush','123-456',24)""")
c.execute("""INSERT INTO credits (id,firstname, phone,amount) VALUES (2,'elaine','1337-1337',18)""")
dbconnect.commit()
c.close()
def print_firstname(phone):
cursor = dbconnect.cursor()
cursor.execute("""SELECT * FROM credits WHERE phone = ?""",(phone,))
data = cursor.fetchone()
firstname = data[1]
cursor.close() # cleanup
print firstname
print "testing existing row"
print_firstname('1337-1337')
print "testing missing row"
print_firstname('nothere')
=>
./soverflow_sqlite.py
testing existing row
elaine
testing missing row
Traceback (most recent call last):
File "./soverflow_sqlite.py", line 31, in <module>
print_firstname('not-in-db')
File "./soverflow_sqlite.py", line 23, in print_firstname
firstname = data[1]
TypeError: 'NoneType' object is not subscriptable
Solution:
Add a check if there was a row returned from your query

Using SQLite3 in Python

I am trying to store some parsed feed contents values in Sqlite database table in python.But facing error.Could anybody help me out of this issue.Infact it is so trivial question to ask!I am newbie!..Anyway thanks in advance!
from sqlite3 import *
import feedparser
data = feedparser.parse("some url")
conn = connect('location.db')
curs = conn.cursor()
curs.execute('''create table location_tr
(id integer primary key, title text ,
updated text)''')
for i in range(len(data['entries'])):
curs.execute("insert into location_tr values\
(NULL, data.entries[i].title,data.feed.updated)")
conn.commit()
curs.execute("select * from location_tr")
for row in curs:
print row
And Error is:
Traceback (most recent call last):
File "F:\JavaWorkspace\Test\src\sqlite_example.py", line 16, in <module>
(NULL, data.entries[i].title,data.feed.updated)")
sqlite3.OperationalError: near "[i]": syntax error
Try
curs.execute("insert into location_tr values\
(NULL, '%s', '%s')" % (data.entries[i].title, data.feed.updated))
the error should be this line
curs.execute("insert into location_tr values\
(NULL, data.entries[i].title,data.feed.updated)")
data.entries[i].title comes from Python. So if you enclose it in double quotes, it becomes a literal string, not a value. It should be something like this:
curs.execute("insert into location_tr values (NULL," + data.entries[i].title +","+data.feed.updated+")")

Categories