I have a function that drops some tables and reinitializes a new set which are working fine, then when it updates the existing table with the following:
self.cursor.execute('''UPDATE beers1 SET (beer_name, og, fg, beer_desc, ibu, glass_type, keg_size)
VALUES (?,?,?,?,?,?,?) where id=1''',("Beer", 1, 1, "Delicious!", 0, "Pint Glass", 640))
Which then gives me:
OperationalError: near "(": syntax error
Any insight would be incredibly helpful. Thanks!
The standard SQL syntax for an UPDATE statement is either:
UPDATE beers1
SET (beer_name, og, fg, beer_desc, ibu, glass_type, keg_size) =
(?, ?, ?, ?, ?, ?, ?)
WHERE id = 1
Or:
UPDATE beers1
SET beer_name = ?, og = ?, fg = ?,
beer_desc = ?, ibu = ?, glass_type = ?, keg_size = ?
WHERE id = 1
You will need to check the SQLite3 manual to see which is supported by SQLite3. The second is almost guaranteed to be supported; the first may not be.
Related
I have a table with 16 columns and I would like to replace the syntax of the executemany method, which is:
# DATABASE is of type sqlite3.Cursor
# data is of type List[List]
DATABASE.executemany(
"INSERT INTO table_name VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", data
)
DATABASE_CONNECTION.commit()
with something more compact. I don't want to repeat the question mark 16 times. Is there a solution for this ?
You can construct the sql statement lik ethis:
sql = "INSERT INTO table_name VALUES (" + ("?," * len(data)).strip(",") + ")"
so that you get "?," repeated as many times as the number of items in data and use it:
DATABASE.executemany(sql, data)
Here is the code that I am trying to execute
db.execute('''UPDATE WARDWINS \
SET map = ?,
SET team1wards = ?,
SET team2wards = ?,
SET mostwards = ?,
SET winningteam = ?,
SET mostwardswin = ?
where matchID = ?''', (dic['mapID'][0], dic['team1wards'], dic['team2wards'], dic['mostwards'], dic['winningteam'], dic['wardswins'], match))
What I want to achieve is to have a single execute command update all the above rows at the same time, but for some reason I cannot seem to figure out how to do just that. I get the following error message:
>>> dic['mostwards'], dic['winningteam'], dic['wardswins'], match))
sqlite3.OperationalError: near "SET": syntax error
So, as it turns out, the answer was nearly correct. (Isn't it always?)
The sqlite3.execute command requires one SET only, and the others were redundant, and created a syntax error. The right way of doing it is as follows:
db.execute('''UPDATE WARDWINS \
SET map = ?,
team1wards = ?,
team2wards = ?,
mostwards = ?,
winningteam = ?,
mostwardswin = ?
where matchID = ?''', (dic['mapID'][0], dic['team1wards'], dic['team2wards'], dic['mostwards'], dic['winningteam'], dic['wardswins'], match))
Trying to pass a variable (a set) into an sqlalchemy query.
Found this: How can I bind a list to a parameter in a custom query in sqlalchemy? But it requires you to know many items there are. The number of entries changes at any given moment.
My previous question went mostly unanswered unfortunately so I figured I'd re-iterate what I'm trying to do here.
Basically, I have this variable: sites = set(db1).intersection(db2) and I'm trying to pass it into this sql alchemy query:
'test': DBSession.query(A_School.cis_site_id.in_(sites)).all(),
But I get invalid syntax errors and invalid parameter type errors...I can't get this thing to do what I want it to do. DB1 and DB2 are, as you mightve guessed, 2 different databases.
db1 = cis_db.query(site.site_id).join(site_tag).filter(site_tag.tag_id.like(202)).all()
db2 = DBSession.query(A_School.cis_site_id).all()
Full error:
ProgrammingError: (ProgrammingError) ('Invalid parameter type. param-index=0 param-type=KeyedTuple', 'HY105') u'SELECT [A_School].cis_site_id IN (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) AS anon_1
FROM [A_School]' ((14639,), (14709,), (14587,), (14966,), (14625,), (14589,), (15144,), (15171,) ... displaying 10 of 18 total bound parameter sets ... (15133,), (14036,))
KeyedTuple is the type of each row returned by SQLAlchemy when not querying one full model. You are making sets of keyed tuples, rather than sets of the single value in each tuple. Should look something like this instead:
db1 = set(x.site_id for x in cis_db.query(site.site_id).join(site_tag).filter(site_tag.tag_id.like(202)))
db2 = set(x.cis_site_id for x in DBSession.query(A_School.cis_site_id))
sites = db1.intersection(db2)
test = DBSession.query(A_School).filter(A_School.cis_site_id.in_(sites)).all()
Assuming you would like to load schools for the sites, how about you try:
DBSession.query(A_School).filter(A_School.cis_site_id.in_(sites)).all()
instead of:
DBSession.query(A_School.cis_site_id.in_(sites)).all()
I try to insert values from variables and values from a list into a sqlite3 database.
Problem: The list is not unpacked, it is used as a single element. What do I miss? Do I have to unpack the list into variables? In the example list_element is a list with six elements
cur.execute("INSERT INTO websites VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
('NULL', qry, strtm, list_element, COUNTER, SPAM, EXCERPT, COLLECTION))
Concatenate the sequences:
cur.execute("INSERT INTO websites VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[None, qry, strtm] + list_element + [COUNTER, SPAM, EXCERPT, COLLECTION])
I built a list here as it is easier to concatenate lists and lists than it is to concatenate tuples and lists (you'd have to convert list_element to a tuple first).
Note that I used None instead of 'NULL'; None is translated to a SQL NULL by Python database adapters.
I'm a rookie at SQLite stuff, and a relative noob in Python. I'm collecting process instrument data for a demonstration application. I've written this code:
from DAQ_Util import *
from sqlite3 import *
print 'Setting up memory-resident SQL database...'
BenchDB = connect(':memory:')
DBTableSetup = BenchDB.cursor()
DBTableSetup.execute("create table ss2000 (timestamp, var1, var2, var3, var4, var5, var6, var7, var8, var9)")
DBTableSetup.close()
# Access data from SS2000 TDL Sensor
instReading = ss2000serialRead('192.168.1.121', 4001, 57)
print instReading
SS2000TabPopulate = BenchDB.cursor()
SS2000TabPopulate.execute("insert into ss2000 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
[instReading(i) for i in instReading])
print '...Done'
The DAQ_Util module is a collection of routines for grabbing process data from instruments. ss2000serialRead is from DAQ_Util and brings in a list of data strings from such an instrument. Some data is reproduced below. All I'm trying to do is write this data to an sqlite table using a list comprehension. It's not working, and I don't know how to interpret the error msg, though I think folks who know more about Python then me may spot the error immediately, or so I hope... :o) A dump of the screen output follows:
$ python SQLite-test.py
Setting up memory-resident SQL database...
Try connecting to serial server...
['2012-08-05 16:52:49.548095', '20.0000', '0.0000', '13.5', '76.60', '8190', '1640', '240', '-13', '79.40']
Traceback (most recent call last):
File "SQLite-test.py", line 17, in <module>
[instReading(i) for i in instReading])
TypeError: 'list' object is not callable
$
Can someone please point out the error?
Thanks!
Thanks Ignacio, here's corrected code:
# Access data from SS2000 TDL Sensor
instReading = ss2000serialRead('192.168.1.121', 4001, 57)
print instReading
SS2000TabPopulate = BenchDB.cursor()
SS2000TabPopulate.execute("insert into ss2000 values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
instReading)
====
Red
instReading is already a sequence.
....execute(..., instReading)