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 ,
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
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 ")
The console prints data
Database table content