Trying to create an SQL database in Python and insert some basic data then return the hex value of the first row of name/age in the database and then print it in python but keep getting tuples error.
Thanks
import sqlite3
conn = sqlite3.connect('Exercise_Ages.sqlite')
cur = conn.cursor()
cur.execute('''DROP TABLE IF EXISTS Ages''')
cur.executescript('''CREATE TABLE Ages (name VARCHAR(128), age INTEGER);
DELETE FROM Ages;
INSERT INTO Ages (name, age) VALUES ('Trudie', 18);
INSERT INTO Ages (name, age) VALUES ('Marley', 26);
INSERT INTO Ages (name, age) VALUES ('Elshan', 17);
INSERT INTO Ages (name, age) VALUES ('Reese', 32);
INSERT INTO Ages (name, age) VALUES ('Lex', 31);
INSERT INTO Ages (name, age) VALUES ('Briagha', 16);''')
conn.commit()
sqlstr = 'SELECT hex(name || age) AS name FROM Ages ORDER BY age DESC LIMIT 10'
for row in cur.execute(sqlstr):
print(str(row[0]), row[1])
cur.close()
Traceback (most recent call last):
File "<ipython-input-63-ddbebe584cc3>", line 22, in <module>
print(str(row[0]), row[1])
IndexError: tuple index out of range
This SELECT hex(name || age) AS name FROM Ages
retrieves a single column for each row, and you're trying to print the second column too with:
print(str(row[0]), row[1])
Just do the following:
print(str(row[0]))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.