Last active
August 16, 2019 10:09
-
-
Save yumaueno/ce2fe1d509652e769e3c4719e4afd339 to your computer and use it in GitHub Desktop.
pythonでデータベース操作
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlite3 | |
dbname = 'trains.db' | |
conn=sqlite3.connect(dbname) | |
c = conn.cursor() | |
# executeメソッドでSQL文を実行する | |
create_table = 'create table toukeilab (id value,name verchar)' | |
c.execute(create_table) | |
# SQL文に値をセットする場合は,Pythonのformatメソッドなどは使わずに, | |
# セットしたい場所に?を記述し,executeメソッドの第2引数に?に当てはめる値をタプルで渡す. | |
sql = 'insert into toukeilab (id, name) values (?,?)' | |
namelist = (1, "uma") | |
c.execute(sql, namelist) | |
conn.commit() | |
select_sql = 'select * from toukeilab' | |
c.execute(select_sql) | |
result=c.fetchone() | |
conn.close() | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment