CONNECT()
import sqlite3
# CREATE a DATABASE IN RAM
db = sqlite3.CONNECT(':memory:')
# CREATE/OPEN a file called mydb.
db = sqlite3.CONNECT('data/mydb')
cursor AND commit()
# GET a cursor object
cursor = db.cursor()
#execute the query
cursor.EXECUTE('''CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT, email TEXT)''')
#commit it TO the DATABASE
db.commit()
ROLLBACK()
#rollback the execution
db.ROLLBACK()
fetchone()
# obtains SOME ROWS FROM a TABLE
cursor.EXECUTE('''SELECT name,email FROM users''')
#retrieve the FIRST ROW
user1 = cursor.fetchone()
#Print the FIRST COLUMN retrieved(USER's name)
print(user1[0])
fetchall()
# obtains some rows from a table
cursor.execute('''SELECT name, email FROM users''')
#retrieve all the rows
all_rows = cursor.fetchall()
for row in all_rows:
print('{0} : {1}'.format(row[0], row[1]))
Comments
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎
0 B
|👍
/👎