程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python---sqlite3 database programming

編輯:Python

As a Python beginner , I want to write a blog to record my growth process , At the same time, I also share my knowledge . The following is a Python Beginners are right Python Some superficial views and personal understanding of language .

''' 1. Import sqlite3 modular 2. Create connection sqlite3.connect() 3. Create cursor object 4. Write create table sql sentence 5. perform sql 6. Close the connection '''
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
print(con)
# Create cursor object 
cur = con.cursor()
# Write create table sql sentence 
sql = '''create table t_person( pno INTEGER primary key autoincrement, pname VARCHAR not null, age INTEGER )'''
try:
# perform sql sentence 
cur.execute(sql)
print(' Table created successfully ')
except Exception as e:
print(e)
print(' Failed to create table ')
finally:
# Close cursor 
cur.close()
# Close the connection 
con.close()
# The import module 
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
# Create cursor object 
cur = con.cursor()
# Write insert sql
sql = 'insert into t_person(pname,age) values(?,?)'
try:
# perform sql
cur.execute(sql,(' Zhang San ',24))
# Commit transaction 
con.commit()
print(' Insert data succeeded ')
except Exception as e:
print(e)
con.rollback()
print(' Insert data failed ')
finally:
# Close cursor connection 
cur.close()
# Close database connection 
con.close()
# The import module 
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
# Create cursors 
cur = con.cursor()
# Write insert sql
sql = 'insert into t_person(pname,age) values(?,?)'
try:
# perform sql
cur.executemany(sql,[(' Zhang San ',23),(' Li Si ',24)])
# Commit transaction 
con.commit()
print(' Insert multiple pieces of data successfully ')
except Exception as e:
print(e)
con.rollback()
print(' Failed to insert multiple pieces of data ')
finally:
# Close cursor connection 
cur.close()
# Close transaction connection 
con.close()
# The import module 
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
# Create cursors 
cur = con.cursor()
# Create a query sql
sql = 'select * from t_person'
try:
cur.execute(sql)
# Get the result set 
person_all = cur.fetchall()
# print(person_all)
for person in person_all:
print(person)
except Exception as e:
print(e)
print(' Failed to query all data ')
finally:
# Close cursor 
cur.close()
# Close the connection 
con.close()
# The import module 
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
# Create cursors 
cur = con.cursor()
# Create a query sql
sql = 'select * from t_person'
try:
cur.execute(sql)
# Get the result set 
person = cur.fetchone()
print(person)
except Exception as e:
print(e)
print(' Failed to query data ')
finally:
# Close cursor 
cur.close()
# Close the connection 
con.close()
# The import module 
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
# Create cursors 
cur = con.cursor()
# Write modified SQL sentence 
sql = 'update t_person set pname=? where pno=?'
# perform sql
try:
cur.execute(sql,(' Zhang San ',1))
print(' Modification successful ')
except Exception as e:
print(e)
print(' Modification failed ')
con.rollback()
finally:
# Close cursor 
cur.close()
# Close the connection 
con.close()
# The import module 
import sqlite3
# Create connection 
con = sqlite3.connect('f:\Python\sqlite3Demo/demo.db')
# Create cursors 
cur = con.cursor()
# Write deleted SQL sentence 
sql = 'delete from t_person where pno=?'
# perform sql
try:
cur.execute(sql,(1,))
# Commit transaction 
con.commit()
print(' Delete successful ')
except Exception as e:
print(e)
print(' Delete failed ')
con.rollback()
finally:
# Close cursor 
cur.close()
# Close the connection 
con.close()

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved