7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonからsqliteに接続

Last updated at Posted at 2020-03-02

#メモリ上にデータベースを作る
テーブルの削除などが不要になるので何度もSQLを実行して試したい時に便利です。

import sqlite3

#conn = sqlite3.connect('test_sqlite.db')
conn = sqlite3.connect(':memory')

curs = conn.cursor()

curs.execute('CREATE TABLE persons(id INTEGER PRIMARY KEY AUTOINCREMENT,name STRING)')

curs.execute('INSERT INTO persons(name) values("Mike")')
curs.execute('INSERT INTO persons(name) values("Nancy")')
curs.execute('INSERT INTO persons(name) values("Jun")')
conn.commit()

curs.execute('SELECT * FROM persons')
print(curs.fetchall())

curs.close()
conn.close()

実行結果:

[(1, 'Mike'), (2, 'Nancy'), (3, 'Jun')]
7
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?