Skip to content

Instantly share code, notes, and snippets.

@brokendish
Created March 27, 2016 07:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brokendish/6e92add463cfbad57986 to your computer and use it in GitHub Desktop.
Save brokendish/6e92add463cfbad57986 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# _*_ coding: utf-8 _*_
import sqlite3 as sq
import sys
#テーブルを削除 ※SQLiteはTruncateはサポートしていないようなのでDROPしてVACCUMする
#データベースに接続
con = sq.connect("sqlite_test.db")
try:
con.execute("delete from test_tbl")
con.execute("vacuum")
con.commit()
con.close()
except:
print ("Unexcepted error:",sys.exc_info()[0])
sys.exit()
#レコードを挿入----------------------------------------
#データベースに接続
con = sq.connect("sqlite_test.db")
# カーソルの作成
cur = con.cursor()
#テーブル作成
#cur.execute("create table test_tbl(id,name);")
#データをINSERT
sql = "insert into test_tbl values('1','python');"
#SQL実行
cur.execute(sql)
#一気にINSERT
cur.executemany("insert into test_tbl values(?,?)",
[("1", "aaaa"),
("2", "bbbb"),
("3", "cccc"),
("4", "dddd")])
con.commit()
con.close()
#レコードを取得------[カラムの位置で取得]----------------------------------
#データベースに接続
con = sq.connect("sqlite_test.db")
# カーソルの作成
cur = con.cursor()
#SQL実行
cur.execute("select * from test_tbl")
#位置で取得
for i in cur:
print i[0], i[1]
con.commit()
con.close()
print "-#"*30 + " [カラムの位置で取得]"
#レコードを取得------[項目名で取得]-----------------------------------------
#データベースに接続
con = sq.connect("sqlite_test.db")
#位置ではなく名前でカラムにアクセス
con.row_factory = sq.Row
# カーソルの作成
cur = con.cursor()
#SQL実行
cur.execute("select * from test_tbl")
#for row in con.execute("select * from test_tbl"):
for row in cur:
print row["id"], row["name"]
con.commit()
con.close()
print "-#"*30 + "[項目名で取得]"
#レコードを取得---Fetch---[カラムの位置で取得]-----------------------------------------
#データベースに接続
con = sq.connect("sqlite_test.db")
fc = con.execute("select * from test_tbl")
#fetchone()で1行ずつ取得
row = fc.fetchone()
print row[0], row[1]
print row[0], row[1]
#全部取得
for al in fc.fetchall():
print al[0], al[1]
print "-#"*30 + "fetch [カラムの位置で取得]"
con.commit()
con.close()
#レコードを取得---Fetch---[項目の位置で取得]-----------------------------------------
#データベースに接続
con = sq.connect("sqlite_test.db")
#位置ではなく名前でカラムにアクセス
con.row_factory = sq.Row
fc = con.execute("select * from test_tbl")
#fetchone()で1行ずつ取得
row = fc.fetchone()
print row["id"], row["name"]
print row["id"], row["name"]
#全部取得
for al in fc.fetchall():
print al["id"], al["name"]
print "-#"*30 + "fetch [項目の位置で取得]"
con.commit()
con.close()
@brokendish
Copy link
Author


SQLiteの準備

・データベース作成
$ sqlite3 sqlite_test.db
・テーブル作成

create table test_tbl(id,name);
.exit
・データベース接続
$sqlite3 sqlite_test.db
.header on
.mode column
select * from test_tbl;

※データベースを作成すると直下に「sqlite_test.db」のデータベースファイルが作成される。
これがDBの本体で、DB全体のバックアップする場合はこのファイルをバックアップすればいいし、
DBの名前を変更したい場合もこのファイル名を変えればよい。

@brokendish
Copy link
Author

結果

$ python sqlite.py
1 python
1 aaaa
2 bbbb
3 cccc
4 dddd
-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-# [カラムの位置で取得]
1 python
1 aaaa
2 bbbb
3 cccc
4 dddd
-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#[項目名で取得]
1 python
1 python
1 aaaa
2 bbbb
3 cccc
4 dddd
-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#fetch [カラムの位置で取得]
1 python
1 python
1 aaaa
2 bbbb
3 cccc
4 dddd
-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#-#fetch [項目の位置で取得]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment