Python

Python - 12. 데이터베이스 - SQLite

꿀먹는매미 2021. 3. 15. 10:44

1. 파이썬 기본문법

2. 데이터 조작

      file io

                    os.path

                    glob

                    open

                    pickle

      mariadb(mysql)

      - mariadb

                   pip install mariadb

                                - 직접연결

                                - 데이터베이스 풀링

                   https://mariadb.com/docs/clients/connector-python/

      - mysql

      *sqlite

                 www.sqlite.org

                 https://www.sqlite.org/datatype3.html

                 https://sqlitebrowser.org         - sqlite 브라우저

 


# sqlite01.py

 

 

 

# sqlite02.py
import sqlite3

conn = sqlite3.connect( 'test.db' )

cursor = conn.cursor()
cursor.execute( 'create table phonebook( name text, phonenum text )' )
cursor.execute( "insert into phonebook values ( 'derick', '010-111-1111' )" )
cursor.close()

print( '데이터베이스에 데이터 입력 성공' )

conn.close()

sqlbrowser로 데이터베이스 데이터 확인해보기

 

commit() 을 해줘야만 데이터가 들어감

sql문과 데이터부분을 나눠서 preparedStatement 구문으로 작성하기

# sqlite03.py
import sqlite3

conn = sqlite3.connect( 'test.db' )

cursor = conn.cursor()
#cursor.execute( "insert into phonebook values ( 'derick', '010-111-1111' )" )

name = 'SangJung'
phonenumber = '010-2222-2222'
sql_insert = 'insert into phonebook values( ?, ? )'
sql_data = (name, phonenumber )

cursor.execute( sql_insert, sql_data )

conn.commit()       # commit()이 들어가야 데이터가 들어감
cursor.close()

print( '데이터베이스에 데이터 입력 성공' )

conn.close()

 

 

p.306 써보기

p.307~308 써보기 

 

patch1 / patch2 예제

 

 

 

1. 우편번호

         파일 - 데이터

 

2. 우편번호 검색기