티스토리 뷰

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()



2. 데이터 입력 

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")



댓글
최근에 올라온 글
«   2024/05   »
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
글 보관함
Total
Today
Yesterday