Tip: After the article is written, the table of contents can be automatically generated. For how to generate it, please refer to the help document on the right.
Article directory
One, MYSQL
The database is equivalent to our commonly used folders, and the data table is equivalent to our commonly used files. Files are stored in folders, that is, data tables are stored in the database. When you want to create a new table, you need to select a database to create it.
2. MySQL management
View existing database
show databases;
Create database
create database database name DEFAULT CHARSET utf8 COLLATE utf8_general_ci;
create database demo2 DEFAULT CHARSET utf8 COLLATE utf8_general_ci ;
Delete database
drop database database name;
drop database demo2;
Enter database
use database name;
use demo1;
Note: To view the data table (show tables;); to create the data table (create table data table name), you need to enter the folder first and then view it, that is, use connection.
Create table
create table tb11(
-> id int primary key, //The primary key cannot be empty, indicating a unique identification
-> name varchar(16) not null, //It cannot be empty, (16 cannot be less)
-> age int null //Yes Is empty (the last item cannot have ",")
-> )default charset=utf8;
create table tb11(
id int primary key,
name varchar(16) not null,
age int null
)default charset=utf8;
Note: -> id int auto_increment primary key, //increment by 1 2… √
Delete table
drop table table name;
drop table tb2;
display table rows
desc table name;
desc tb1;
Insert data
insert into table name(id,name,age) values(1,“aa”,20),(2,“bb”,21);
insert into tb1(id,name,age) values(1,"aa",20),(2,"bb",21);
View data in table
select * from table name;
select * from tb1;
delete data
delete from 表名字;
delete from 表名字 where 条件 ;
eg: delete from tb33 where id = 1;
delete from tb33 where id = 1 and name=”aa”;
delete from tb33 where id <=4;
delete from tb1 where id=1;
change the data
update table name set column=value;
update table name set column=value, column=value;
update table name set age=age+10 where condition;
update tb1 set name="cc";
3. Python code performs database operations
Open pycharm -- create a new project -- terminal writes pip (version number) install pymysql
Dynamically created
(note the indentation)
import pymysql
while True:
username=input("用户名:")
if username.upper()=='Q':
break
password=input("密码:")
mobile=input("手机号:")
# 1.连接MySQL
conn=pymysql.connect(host="127.0.0.1",port=3306,user='root',passwd="12345",charset='utf8',db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 2.发送指令 (不能用字符串格式化去做MySQL的拼接,有安全隐患)
sql="insert into admin(username,password,mobile) values(%s,%s,%s)"
cursor.execute(sql,[username,password,mobile])
conn.commit()
# 3.关闭
cursor.close()
conn.close()
Among them, passwd="12345" represents the password to enter the database, db='unicom' is the name of the new database, and admin is the name of the new table. You can set it yourself.
Query data
All data: fetchall() If there is no data, it is an empty list.
The first data that meets the conditions: fetchone() If there is no data, it is none.
import pymysql
# 1.连接MySQL
conn=pymysql.connect(host="127.0.0.1",port=3306,user='root',passwd="12345",charset='utf8',db='unicom')
cursor = conn.cursor(cursor=pymysql.cursors.DictCursor)
# 2.执行查询的指令
cursor.execute("select * from admin ",)
# %s是exceute的占位符,
rew=cursor.fetchone()
# print(data_list)
# 得出的结果是以字典形式展示列表中的数据
print(rew)
# 3.关闭
cursor.close()
conn.close()
delete data
cursor.execute("delete from admin where id =%s",[8,])
The comma after 8 cannot be omitted.
change the data
cursor.execute("update admin set mobile=%s where id =%s",["49999999999",7,])
The comma after 7 cannot be omitted.