import mysql.connector
from mysql.connector import errorcode
connection_config = {
"user": "用户名",
"password": "暗码",
"host": "ip地址",
"database": "数据库名"
}
values02 = {} #在Python中查找到的mysql数据是由元组组成的list列表,所以寄存的时分设定值的特点为list特点,也能够不必设置;
values01 = {}
values03 = {}
try:
cnx = mysql.connector.connect(**connection_config)
# fetchwarings的运用-----01
cnx.get_warnings = True #fetchwaring()运用时必需求设置的参数,默许是false
cursor = cnx.cursor()
cursor.execute("select 'a'+1;")
print(cursor.fetchall())
print(cursor.fetchwarnings())
print("#############################")
# -----01
query01 = "select 【任何格局的字段】,DATE_FORMAT(【时刻格局的字段名】,'%Y-%m-%d') from 【表名】 limit 3;"
cursor.execute(query01)
# 榜首种fetchone()运用-----02
row01 = cursor.fetchone()
while row01 is not None:
print(row01)
row01 = cursor.fetchone()
print("############################")
# -----02
# 第二种fetchone()运用-----03
query02 = "select DATE_FORMAT(【时刻格局的字段名】,'%Y-%m-%d') from 【表名】 limit 4;"
cursor.execute(query02)
for row02 in cursor:
print(row02)
print("############################")
# -----03
# fetchmany()运用-----04
query03 = "select 【字段名】 from 【表名】 limit 5;"
cursor.execute(query03)
values01 = cursor.fetchmany(size=2)
print(values01)
# -----04
# fetchall()运用------05
values02 = cursor.fetchall()
print(values02)
print("Warning: 开端-- {} --完毕".format(cursor.fetchwarnings()))
# -----05
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password.")
print(err)
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exists!")
else:
print("Error:")
print(err)
else:
cursor.close()
cnx.close()
# for i in values02:
# print(i)
# fetchmany()函数:默许抓出检索出的数据的榜首行数据;括号里添加一个size=n的值,它就会抓出前n条记载。
# fetchone()函数: 默许抓出检索出的数据的一行,能够运用while句子循环抓取,也能够将cusor作为迭代器抓取;
# #注:假如用同一个衔接目标去履行新的sql句子,必需求让这个fetchone()函数抓取完它本该显现的一切数据才干被下一个sql运用其用过的衔接目标。
# fetchall()函数:默许抓取检索到的一切行数据信息,一次性获取;
# fetchwarning()函数:能够抓取到正告的信息,需求对生成的衔接目标做一次敞开动作
---------------------
以上的信息的参阅来自与官网供给的