티스토리 뷰
1. 먼저 데이터베이스를 만든다.
import sqlite3
conn = sqlite3.connect('tel.db')
c = conn.cursor()
c.execute(" create table tel ( id integer primary key autoincrement, name text, tel text, addr text, input_time text, memo text ) ")
# Unlike DDL, DML (Data Manipulation Language) commands need to be commited/rolled back.
c.close()
import sqlite3, os
import time
conn = sqlite3.connect('tel.db')
c = conn.cursor()
name=input("이름 : ")
tel=input("전화번호 : ")
addr=input("주소 : ")
memo=input("메모 : ")
input_time=str(time.asctime(time.localtime(time.time())))
c.execute (" insert into tel (name, tel, addr, input_time, memo) values (' " +name+ "' , ' "+tel+"', '"+addr+"', '"+input_time+ "' , ' "+memo+"')")
conn.commit()
#insert문은 DML이므로 commit를 해줘야 된다.
for row in c.execute('SELECT * FROM tel'):
print (row)
c.close()
os.system("pause")
3. 데이터 확인
import sqlite3, os
conn = sqlite3.connect('tel.db')
c = conn.cursor()
os.system("cls")
print("")
print("No \t 성명 \t 전화번호 \t 주소 \t 메모 \t 입력일자")
print ("----------------------------------------------------------------------------")
for row in c.execute('SELECT * FROM tel order by id desc'):
print ( str(row[0])+"\t"+str(row[1])+"\t"+str(row[2])+"\t"+str(row[3])+"\t"+str(row[5])+"\t"+str(row[4]) )
print ("----------------------------------------------------------------------------")
c.close()
print("")
os.system("pause")