Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- PostgreSQL
- DBFilter
- cursor()
- subquery
- insert_into
- query
- 밴쿠버응급실
- SubqueryFilter
- DB
- onetoone
- insertOne
- Collections
- 서브쿼리
- python
- javascript
- sql
- MySQL
- PostgresDB
- postgres
- MongoDB
- 자바스크립트
- 파이썬
- PreparedStatement
- 데이터베이스
- sqlite3
- Join
- statement
- 몽고디비
- DATABASE
- python데이터베이스연동
Archives
- Today
- Total
새벽코딩
[Python-DB] 3. 데이터의 입력과 조회 -3 (create product table) 본문
Programming/Python
[Python-DB] 3. 데이터의 입력과 조회 -3 (create product table)
midnightcoder 2022. 12. 23. 07:35https://midnightcoding.tistory.com/70
https://midnightcoding.tistory.com/71
이전 2개의 글에서 다룬 내용을 바탕으로 새로운 product table을 만든다.
# naverDB에 productTable 을 만들고, 열의 개수 4개로 하는데 구체적인 열 이름은
# pCode char(5), pName char(20), price int, amount int 와 같이 한다.
# 테이블을 만들고 난 뒤 사용자로부터 3개의 데이터를 입력 받아서 출력한 결과는
# 아래와 같다. 하여, 아래와 같이 출력이 되도록 파이썬 프로그램으로 작성하시오.
# 제품코드 제품명 가격(만) 재고수량
# ----------------------------------------
# p001 노트북 110 5
# p002 마우스 3 22
# p003 키보드 2 11
# ----------------------------------------
import sqlite3
# 전역 변수 선언
con, cur = None, None # 연결자, 커서를 저장하는 변수 초기화 (객체여서 None으로 초기화)
pCode, pName, price, amount = "", "", "", ""
sql = ""
if __name__ == "__main__":
con = sqlite3.connect("c:/PythonDB/naverDB")
cur = con.cursor()
# create table
cur.execute("drop table if exists productTable")
cur.execute("create table productTable(pCode char(5), pName char(20), price int, amount int)")
# input from user, and insert into db by query
while True:
pCode = input("product code ==> ")
if pCode == "":
break
pName = input("product name ==> ")
price = input("product price ==> ")
amount = input("product amount ==> ")
#input using preparedStatement way
cur.execute("insert into productTable(pCode, pName, price, amount) values(?,?,?,?)",
(pCode, pName, price, amount))
# cur.execute("select * from productTable")
# while True:
# row = cur.fetchone()
# if row == None:
# break
# pCode = row[0]
# pName= row[1]
# price = row[2]
# amount = row[3]
# print("%5s %15s %15s %5d" % (pCode, pName, price, amount))
con.commit()
con.close()
다음 글에서는 조회하는 방법에 대해서 다뤄볼 예정이다.
'Programming > Python' 카테고리의 다른 글
Python 형식지정자(%s) 기록 (0) | 2022.12.30 |
---|---|
[Python-DB] 4. 데이터의 조회 (0) | 2022.12.23 |
[Python-DB] 2. 데이터의 입력과 조회 -2 (실제 user input 코드) (0) | 2022.12.23 |
[Python-DB] 1. 데이터의 입력과 조회 (0) | 2022.12.22 |
파이썬 operator (연산자) 및 math 라이브러리 (0) | 2022.12.19 |