Skip to content

Instantly share code, notes, and snippets.

@yumaueno
Last active August 16, 2019 10:09
Show Gist options
  • Save yumaueno/ce2fe1d509652e769e3c4719e4afd339 to your computer and use it in GitHub Desktop.
Save yumaueno/ce2fe1d509652e769e3c4719e4afd339 to your computer and use it in GitHub Desktop.
pythonでデータベース操作
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