The fifth question of the interview:
Part 1 - Test Site:
Operate the SQLite database:
- Create SQLite database;
- insert records into the table;
- Other database operations.
Part 2 - Interview Questions:
1. Interview question 1: How to create a SQLite database?
2. Interview question 2: How to insert data into SQLite table?
3. Interview question 3: How to query the data in the SQLite table?
Part 3 - Analysis:
One of the interview questions to create a SQLite database:
# coding=utf-8
# _author__ = 孤寒者
import sqlite3
import os
dbPath = 'data.sqlite'
if not os.path.exists(dbPath):
conn = sqlite3.connect(dbPath)
c = conn.cursor()
c.execute('''create table persons
(id int primary key not null,
name text not null,
age int not null,
address char(100),
salary real);''')
conn.commit()
conn.close()
print('创建数据库成功')
-
We have successfully created the sql database through the above operations and created a table in it.
-
In order to view the table we created, we can use SqliteStudio, which is a Sqlite database visualization tool and a must-have software for developing applications using Sqlite database. The software does not need to be installed. After downloading, unzip it and use it. It is very small but very useful. , the green Chinese version. I like using this over other SQLite management tools. It is very convenient and easy to use, without a single executable file to be installed, and supports Chinese.
-
Official download address: here .
-
I may not be able to download it, so I have already downloaded one here (version 3.3.3), and the WeChat public account [Lone Cold Person] can reply to [sqlitestudio] in the background~
Interview question 2: Insert data into SQLite table:
# coding=utf-8
# _author__ = 孤寒者
import sqlite3
dbPath = 'data.sqlite'
conn = sqlite3.connect(dbPath)
c = conn.cursor()
# 首先将表中数据全部删除
c.execute('delete from persons')
# 插入数据
c.execute('''
insert into persons(id,name,age,address,salary)
values(1, '孤寒者', 18, 'China', 9999)
''')
c.execute('''
insert into persons(id,name,age,address,salary)
values(2, '小张', 55, 'China', 9)
''')
conn.commit()
print('insert success')
Interview question 3: Query data in SQLite table:
# coding=utf-8
# _author__ = 孤寒者
import sqlite3
dbPath = 'data.sqlite'
conn = sqlite3.connect(dbPath)
c = conn.cursor()
persons = c.execute('select name,age,address,salary from persons order by age')
# 打印查询结果发现是个Cursor对象(可迭代对象)
print(type(persons))
result = []
for person in persons:
value = {
}
value['name'] = person[0]
value['age'] = person[1]
value['address'] = person[2]
result.append(value)
conn.close()
print(type(result))
print(result)
# 我们也可以使用前面学习的json模块使这个list类型的result转为字符串类型
# 网络传输需要使用字符串类型
import json
resultStr = json.dumps(result, ensure_ascii=False)
print(resultStr)
Part 4 - Conclusion:
-
The SQLite database can be manipulated using the API in the sqlite3 module, which is a built-in module of Python and does not need to be installed separately.
-
If you want to learn SQLite database systematically, you can watch the rookie tutorial to learn:
rookie tutorial: SQLite tutorial