0% found this document useful (0 votes)
5 views

SQLAlchemy Hands On

The document contains Python code that uses SQLAlchemy to interact with a SQLite database. It demonstrates creating a 'players' table, inserting player data, reading the data, updating a player's runs, and deleting a player from the table. The results of each operation are printed and saved to hidden text files.

Uploaded by

arif895178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

SQLAlchemy Hands On

The document contains Python code that uses SQLAlchemy to interact with a SQLite database. It demonstrates creating a 'players' table, inserting player data, reading the data, updating a player's runs, and deleting a player from the table. The results of each operation are printed and saved to hidden text files.

Uploaded by

arif895178
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

###################################################################################

from sqlalchemy import create_engine

db_string = "sqlite:///tests.db"

db = create_engine(db_string)

#create
db.execute("CREATE TABLE players (plyid text, plyname text, runs text)")
db.execute("INSERT INTO players (plyid, plyname, runs) VALUES('10001',
'ply1','100'), ('10002' , 'ply2', '80'),('10003','ply3','65'),
('10004','ply4','95'),('10005','ply5','99')")

# Read
result_set=db.execute("SELECT * FROM players")

s=list()
for r in result_set:
s.append(r)

#Update

db.execute("UPDATE players SET runs='100' WHERE plyname='ply5'")


update=db.execute("SELECT * FROM players")
q=list()
for r in update:
q.append(r)

#Delete

db.execute("DELETE FROM players WHERE plyname='ply5'")


delete=db.execute("SELECT * FROM players")
e=list()
for r in delete:
e.append(r)

print(s)
print(q)
print(e)
s=str(s)
q=str(q)
e=str(e)
with open(".hidden.txt",'w') as f:
f.write(s)

with open(".hidden1.txt",'w') as f:
f.write(q)
with open(".hidden2.txt",'w') as outfile:
outfile.write(e)

###################################################################################

You might also like