-
[Python] SQLite3 Cursor.executemany() - iterator를 이용한 레코드 추가Python/SQLite 2018. 5. 18. 23:37
Cursor.execute를 이용한 레코드 입력
import sqlite3 conn = sqlite3.connect(':memory:') cur = conn.cursor() cur.execute('CREATE TABLE Score(nation, point)') cur.execute('INSERT INTO Score (nation, point) VALUES(?,?)',('kr',90)) cur.execute('INSERT INTO Score (nation, point) VALUES(?,?)',('jp',80)) cur.execute('INSERT INTO Score (nation, point) VALUES(?,?)',('cn',85)) cur.execute('INSERT INTO Score (nation, point) VALUES(?,?)',('cn',85)) cur.execute('select * from Score') for row in cur: print(row)
('kr', 90) ('jp', 80) ('cn', 85) ('cn', 85)
Cursor.executemany를 이용한 레코드 입력. (리스트, 튜플)
import sqlite3 conn = sqlite3.connect(':memory:') cur = conn.cursor() cur.execute('CREATE TABLE Score(nation, point)') value = (('kr',90),('jp',80),('cn',85),('us',80)) # list, tuple cur.executemany('INSERT INTO Score VALUES(?,?)', value) cur.execute('select * from Score') for row in cur: print(row)
('kr', 90) ('jp', 80) ('cn', 85) ('us', 80)
Cursor.executemany를 이용한 레코드 입력. (딕셔너리)
import sqlite3 conn = sqlite3.connect(':memory:') cur = conn.cursor() cur.execute('CREATE TABLE Score(nation, point)') value = {'kr':90,'jp':80,'cn':85,'us':80} cur.executemany('INSERT INTO Score VALUES(?,?)', value.items()) cur.execute('select * from Score') for row in cur: print(row)
('us', 80) ('kr', 90) ('jp', 80) ('cn', 85)
'Python > SQLite' 카테고리의 다른 글
sqlitestudio download (0) 2019.01.24 [Python] sqlite3 iterdump( ) - SQL 쿼리 형식으로 데이터베이스를 덤프 (0) 2019.01.02 [Python] SQLite3 distinct 중복제거 (0) 2018.08.15 [SQLite3] 내장 집계함수 (max, min, avg, sum) (0) 2018.05.18 [Python] API SQLite (0) 2018.04.23