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)
Related
I'm wondering about if it's possible to get data from a database with this query: SELECT Name FROM fraleon WHERE Relay = "value from a string"?
When I try to put in a string in the query it comes with a message that the column doesn't exists.
I have just started with programming, so hope my question isn't to stupid ;-) I'm trying to make an app where i have a QtableWidget and a QScrollbar where the value from the scrollbar (1 - 100) is used to change to the next relay.
con = sqlite3.connect("C:\Leonclient\LocalDB\LeonDBlite.db")cur = con.cursor()
value = 5
for row in cur.execute('SELECT Name FROM fraleon where Relay = value'):
print(row)
Traceback (most recent call last):
File "C:\Users\xxx\AppData\Local\Temp/ipykernel_10292/3072186007.py", line 1, in <module>
for row in cur.execute('SELECT Name FROM fraleon where Relay = (Value)'):
OperationalError: no such column: Value
You may need to concatenate the query string with variable value otherwise it will be considered literally 'value'
for row in cur.execute('SELECT Name FROM fraleon where Relay = ' + str(value)):
You can try to write the query -
SELECT Name FROM fraleon WHERE Relay like '%somevalue%'
That way you will specify a sub-string of the Relay's column values
This question already has answers here:
How do I get around not being able to parse a table name into a python sqlite query?
(3 answers)
Closed 3 years ago.
I am trying to pass the result of a user input into an SQL query, like so:
def select_rows(conn):
table_choice = input("Choose a table: ")
cur = conn.cursor()
cur.execute("SELECT * FROM %s", table_choice)
rows = cur.fetchall()
for row in rows:
print(row)
but I get this Error:
Database Path: C:\Users\Morgan\Desktop\SQL\chinook.db
Choose a table: album
Traceback (most recent call last):
File "C:\Users\Morgan\Desktop\SQL\SQL.py", line 27, in <module>
select_rows(conn)
File "C:\Users\Morgan\Desktop\SQL\SQL.py", line 20, in select_rows
cur.execute("SELECT * FROM %s", table_choice)
sqlite3.OperationalError: near "%": syntax error
Any suggestions?
You can use f-string as well:
cur.execute(f"SELECT * FROM {table_choice}")
But be careful with user's input and query. You should write a function to validate the input.
you need to format the query before executing it...:
cur.execute("SELECT * FROM {}".format(table_choice))
cur.execute("SELECT * FROM ?", (table_choice,))
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})'
# ^^^^^^
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.]
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