py파일(파이썬)을 splite3와 같은 경로에 저장
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | #create_table.py import sqlite3 #데이터베이스 만들기 con = sqlite3.connect( 'testDB' ) cur = con.cursor() #테이블 만들기 cur.execute( """ create table phonebook_0 (name char(32), phone char(32), email char(64)primary key) """ ) #테이블을 닫고 데이터베이스를 종료한다. cur.close() con.close() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #delete_record.py import sqlite3 #데이터베이스 만들기 con = sqlite3.connect( 'testDB' ) cur = con.cursor() #데이터입력 cur.execute( """ delete from phonebook_0 where email=? """ ,( 'kim@hanmail.net' ,)) con.commit() cur.execute( "select name, phone, email from phonebook_0 " ) rows = cur.fetchall() for row in rows: print ( "name : {0},phone : {1},email : {2}" . format (row[ 0 ],row[ 1 ],row[ 2 ])) cur.close() con.close() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #inserte_record.py import sqlite3 #데이터베이스 만들기 con = sqlite3.connect( 'testDB' ) cur = con.cursor() #데이터입력 cur.execute( """ insert into phonebook_0 (name, phone, email) values(?,?,?) """ ,( '김동' , '030-124-4567' , 'kon@naver.com' )) #id값 출력 id = cur.lastrowid print ( id ) #데이터입력 cur.execute( """ insert into phonebook_0 (name, phone, email) values(?,?,?) """ ,( '김차동' , '010-154-4567' , 'young@naver.com' )) #id값 출력 id = cur.lastrowid print ( id ) #데이터를 저장하고 종료한다. con.commit() cur.close() con.close() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #select_record.py import sqlite3 #데이터베이스 만들기 con = sqlite3.connect( 'testDB' ) cur = con.cursor() #데이터입력 cur.execute( "select name, phone, email from PHONEBOOK_0" ) rows = cur.fetchall() for row in rows: print ( "name: {0},phone:{1},email:{2}" . format (row[ 0 ],row[ 1 ],row[ 2 ])) #종료한다. cur.close() con.close() |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #update_record.py import sqlite3 #데이터베이스 만들기 con = sqlite3.connect( 'testDB' ) cur = con.cursor() #데이터입력 cur.execute( """ update phonebook_0 set phone=?,email=? where name=? """ ,( '010-0000-0000' , 'kim@hanmail.net' , '홍길동' )) con.commit() cur.execute( """ select name, phone, email from phonebook_0 where name=? """ ,( '홍길동' ,)) rows = cur.fetchall() for row in rows: print ( "name : {0},phone : {1},email : {2}" ,fowmat(row[ 0 ],row[ 1 ],row[ 2 ])) cur.close() con.close() |
'먼지 낀 책장사이 > Python' 카테고리의 다른 글
파이썬으로 메일보내기 SMTP (0) | 2016.11.08 |
---|---|
SQLite (0) | 2016.10.18 |
쓰레드 (0) | 2016.10.11 |
상속, 오버라이딩, 툴킷 (0) | 2016.09.27 |
Raw파일 읽기, 클래스, 생성자 (0) | 2016.09.20 |