data = cursor.fetchone()
print(“Database Version:%s” % data)
cursor.close() # Close cursor
conn.close() # Close the connection
[](https://gitee.com/vip204888/java-p7) Create database ( Database must exist )
=================================================================================
Because you need to fill in the name of the database to be connected when connecting to the database , So creating a database can only : Delete existing database , Recreate , amount to ** clear database ** The role of .
cursor.execute(“drop database if exists test”) # If the database already exists , Then delete and recreate
sql = “create database test”
cursor.execute(sql)
[](https://gitee.com/vip204888/java-p7) Create data table
========================================================================
cursor.execute(“drop table if exists employee”) # If the data table already exists , Then delete and recreate
sql = “”"
CREATE TABLE employee (
id VARCHAR(20) NOT NULL,
name VARCHAR(20),
age INT,
income FLOAT
)
“”"
cursor.execute(sql)
[](https://gitee.com/vip204888/java-p7) The insert
=======================================================================
sql = “INSERT INTO employee VALUES (‘1’, ‘ Zhang San ’, 20, 5000)”
try:
cursor.execute(sql)
# Submit all operations of the current cursor
conn.commit()
except:
print("expection!")
conn.rollback()
sql = “SELECT* FROM employee”
cursor.execute(sql)
data = cursor.fetchall()
print(data)
[](https://gitee.com/vip204888/java-p7) Query operation
=======================================================================
Only one piece of data is queried :`cursor.fetchone()`
sql = “SELECT* FROM employee”
cursor.execute(sql) # The return value is the number of data queried
data = cursor.fetchone() # Query a piece of data
print(data)
Query all data in the database :`cursor.fetchall()`
sql = “SELECT * FROM employee”
cursor.execute(sql) # The return value is the number of data queried
data = cursor.fetchall() # Query a piece of data
print(data)
Query the data of the specified condition : The query meets the conditions `income > 5000` The data of .
sql = " SELECT * FROM employee WHERE income > ‘%d’ " % (5000)
cursor.execute(sql) # The return value is the number of data queried
data = cursor.fetchone()
print(data)
[](https://gitee.com/vip204888/java-p7) update operation
=======================================================================
sql = " UPDATE employee SET age = age + 1 WHERE age < ‘%d’ " % (25)
try:
cursor.execute(sql)
conn.commit()
except:
print("expection!")
conn.rollback()
sql = “SELECT* FROM employee”
cursor.execute(sql)
data = cursor.fetchall()
print(data)
[](https://gitee.com/vip204888/java-p7) Delete operation
=======================================================================
sql = "DELETE FROM employee WHERE age > ‘%d’ " % (30)
try:
cursor.execute(sql)
conn.commit()
except:
print("exce[tion!")
conn.rollback()
# View the updated results
sql = “select * from employee”
cursor.execute(sql)
data = cursor.fetchone()
print(data)
[](https://gitee.com/vip204888/java-p7) Comprehensive case
=======================================================================
Make sure there are... In the database `test` surface :
import pymysql
import traceback
from time import sleep
class PyMySQL(object):
create_table = """
CREATE TABLE stu (
id INT not null PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(255) NOT NULL,
age INT,
sex VARCHAR(255)
) DEFAULT CHARSET = utf8
"""
select = 'SELECT * FROM stu'
update = 'UPDATE stu SET name = " Mingming " WHERE id=2'
delete = 'DELETE FROM stu WHERE id=9'
insert = 'INSERT INTO stu(name, age, sex) VALUES("%s", "%d", "%s")' % (' Xiao Ming ', 2, " male ")
def __init__(self, host, user, pwd, db):
self.conn = pymysql.connect(host, user, pwd, db)
self.cursor = self.conn.cursor()
print(" Database connection successful !")
def closeAll(self):
self.conn.close()
self.cursor.close()
print(" Resource release completed !")
def create_table_func(self):
self.cursor.execute("DROP TABLE IF EXISTS stu")
self.cursor.execute(PyMySQL.create_table)
print(' The data table is created ')
def insert_date(self):
try:
self.cursor.execute(PyMySQL.insert)
self.conn.commit()
print(" Data insertion successful !")
except:
print(traceback.format_exc())
self.conn.rollback()
print(" Data insertion failed !")
def update_data(self):
try:
self.cursor.execute(PyMySQL.update)
self.conn.commit()
print(" Data updated successfully !")
except:
print(traceback.format_exc())
self.conn.rollback()
print(" Data update failed !")
Many people have been interviewing recently , I have also collated a lot of interview materials , There are also other big factories . I hope I can help you .
The latest interview questions
The answers to the above interview questions are all organized into document notes .
I also sorted out some interview materials & newest 2021 I collected some real interview questions from big factories , If you need anything, you can Click here , Free access
Latest finishing e-book
The latest collation of large factory interview documents
llback()
print(" Data update failed !")
Many people have been interviewing recently , I have also collated a lot of interview materials , There are also other big factories . I hope I can help you .
The latest interview questions
[ Outside the chain picture transfer in …(img-jAlAjnOL-1628508712714)]
The answers to the above interview questions are all organized into document notes .
I also sorted out some interview materials & newest 2021 I collected some real interview questions from big factories , If you need anything, you can Click here , Free access
Latest finishing e-book
[ Outside the chain picture transfer in …(img-YDrennRn-1628508712718)]
The latest collation of large factory interview documents
[ Outside the chain picture transfer in …(img-NVwYQ3gS-1628508712720)]
The above is the whole content of this paper , I hope it will be helpful for your study , I also hope that you can support .