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

Problems of linking SQLite database with Python

編輯:Python

One 、 Create database

establish sqlite Code of the database

import sqlite3
conn = sqlite3.connect("test.db")
print(" Successfully created database ")

After running the code, the file bar on the left will appear “test.db” file ,

Two 、 Linked database

View -> Tool window ->Database

The right side of the editor appears Database, Click the add button

Click the route selection button , Find the created “test.db” file , Choose


Be careful :Download When the download , You may be prompted that the download failed , Try twice more and you'll always download it
At this point, the database is linked

3、 ... and 、 Addition, deletion and search of database

1、 Add header

c = conn.cursor() # To obtain the cursor 
sql = ''' create table company (id int primary key not null, name text not null, age int not null, address char(50), salary real); '''
c.execute(sql) # perform sql sentence 
conn.commit() # Submit database operations 
conn.close() # Close database links 
print(" Table created successfully ")

2、 insert data

conn = sqlite3.connect("test.db")
print(" Database opened successfully ")
c = conn.cursor() # To obtain the cursor 
sql1 = ''' insert into company (id,name,age,address,salary) values (1,' Zhang San ',32," Chengdu ",8000); '''
sql2 = ''' insert into company (id,name,age,address,salary) values (2,' Li Si ',30," Shenzhen ",15000); '''
c.execute(sql1) # perform sql sentence 
c.execute(sql2)
conn.commit() # Submit database operations 
conn.close() # Close database links 
print(" Successfully inserted data ")

3、 Find data

conn = sqlite3.connect("test.db")
print(" Database opened successfully ")
c = conn.cursor() # To obtain the cursor 
sql = ''' select id,name,address,salary from company '''
cursor = c.execute(sql) # perform sql sentence 
for row in cursor:
print("id = ",row[0])
print("name = ",row[1])
print("address = ",row[2])
print("salary = ",row[3],"\n")
conn.close() # Close database links 
print(" Successfully found data ")

Four 、 Running results

The console prints data

Database table content


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