SQLite

[code lang=”python” title=””]
import sqlite3

con = sqlite3.connect(‘sample.db’) #データベースへ接続、無ければ作製

cur = con.cursor() #カーソルオブジェクト作製

sql = "create table fruits(name text, price text);" #Table作製
cur.execute(sql)

sql_2 = "insert into fruits values(‘apple’, ‘100yen’)" #レコード挿入
cur.execute(sql_2)

cur.executemany("insert into fruits values(?, ?)", [(‘orange’, ‘150yen’), (‘banana’, ‘200yen’)]) # レコードを一括挿入

sql_3 = "select * from fruits"
cur.execute(sql_3) # select文をexecute()に渡す
for row in cur: # レコードを出力する
print(row[0], row[1])

con.commit() #コミットする

con.close()
[/code]

INTEGER PRIMARY KEY AUTOINCREMENT:自動的に番号を割り振る

[code lang=”python” title=””]
sql = "create table fruits(id INTEGER PRIMARY KEY AUTOINCREMENT, price text);"
cur.execute(sql)

#この場合、テーブル作成ごとに第一カラムに番号が割り振られます
[/code]

カラム1つに値を入れる

[code lang=”python” title=””]
t = ‘200yen’
cur.execute("insert into fruits(price) VALUES(?)",(t,))
[/code]

カラム2つに値を入れる

[code lang=”python” title=””]
cur.execute("insert into fruits values(?,?)",(‘apple’,’100yen’))

または

p = "insert into fruits values(?, ?)"
cur.execute(p, (‘orange’, ‘200yen’))

[/code]

複数レコードを挿入

[code lang=”python” title=””]
data = [
(‘grape1′,’100yen’),
(‘grape2′,’200yen’),
(‘grape3′,’300yen’),
]
cur.executemany("insert into fruits values(?,?)", data)
[/code]

複数レコードの更新

[code lang=”python” title=””]

sql = "update tweet set word = ‘Yamaoka’ where id = 2;"
cur.execute(sql)

#sql = "update テーブル名 set カラム名 = 値 where 条件;"

[/code]

レコードの削除

[code lang=”python” title=””]
cur.execute(‘delete from fruits where name=?’, (‘grape1’,))

#カーソルオブジェクト.execute(‘delete from テーブル名 where 条件指定)
[/code]

テーブルの中身を削除

[code lang=”python” title=””]
cur.execute(‘delete from fruits’)

#カーソルオブジェクト.execute(‘delete from テーブル名’)
[/code]

テーブルの削除

[code lang=”python” title=””]

cur.execute(‘drop table fruits’)

#カーソルオブジェクト.execute(‘drop table テーブル名’)

[/code]

テーブルがすでに存在しなければ作製

[code lang=”python” title=””]
sql = "create table if not exists fruits(name text, price text);"
cur.execute(sql)
[/code]

テーブルがすでに存在しなければ作製②

[code lang=”python” title=””]
cur.execute("""
SELECT COUNT(*) FROM sqlite_master
WHERE TYPE=’table’ AND name=’テーブル名’
""")

if cur.fetchone()[0] == 0:
sql = "create table テーブル名(id INTEGER PRIMARY KEY AUTOINCREMENT, word text);"
cur.execute(sql)
print(‘create’)
[/code]

条件を指定して参照

[code lang=”python” title=””]
cur.execute("select * from fruits where id >= 2") #2以上
select_data = cur.fetchall()

for row in select_data:
print(row[1])

または

k = "キーワード"
data = cur.execute("select * from fruits where keyword = ?",(k,))

for row in data:
print(row[1])
word_list.append(row[1])

[/code]

条件を指定して参照(変数使用)

[code lang=”python” title=””]
num = 2

cur.execute("select * from fruits where id >= ?",(num,))
select_data = cur.fetchall()

for row in select_data:
print(row[1])
[/code]

条件を指定+最大値を求める

[code lang=”python” title=””]

q = "キーワード"
sql = "select max(カラム名) from テーブル名 where カラム名(条件指定対象) = ?"

data = cur.execute(sql,(q,))

for a in data:
print(a)
[/code]

条件を指定して参照(変数使用)②

[code lang=”python” title=””]

hoge = "キーワード"

sql = "select * from テーブル名 where カラム名 = ?"

data = cur.execute(sql,(hoge,))

for a in data:
print(a[2])

[/code]

条件を指定して最初の1件のみ参照(変数使用)

[code lang=”python” title=””]

q = "キーワード"

#data = cur.execute("select * from fruits where keyword = ?",(q,))

data = cur.execute("select * from fruits where keyword = ? limit 1",(q,))

[/code]

あるレコードの存在の確認

[code lang=”python” title=””]

q = キーワード

data = cur.execute("select exists(select * from fruits where screen_name = ?)",(q,))

for row in data:
#row = タプル型
if row[0] == False:
print("レコードは存在しません")
else:
print("レコードは存在します")

[/code]

カラムの追加

[code lang=”python” title=””]

cur.execute("alter table fruits add column image_id integer")

#alter table テーブル名 add column カラム名 値の種類

[/code]

[code lang=”python” title=””]

[/code]

SQLite

Posted by iser