python将数组传入mysql_通过python将文件中的数据传输到MySQL,传到...
python将数组传⼊mysql_通过python将⽂件中的数据传输到
MySQL,传到,mysql
我们需要⼏个包来实现这⼀功能,下⾯我和详细讲⼀下这⼏个包
⼀、pymysql
要传数据到数据库中肯定先要连接数据库
①连接数据库
db = t('localhost', 'root', '000000', 'sys')
# 等价于,因为怕参数顺序错误,可以通过以下形式
db = t(host='localhost', user='root', password='000000', database='sys')
# 等价于,也可以将参数都传到⼀个字典⾥,作为参数传递给函数
DATABASE = {
'host': 'localhost',
'database': 'girls',
'user': 'root',
'password': '000000'
}
db = t(**DATABASE)
下⾯是连接数据库connect的所有参数,⼤家写的时候可以参考⼀下
要完成⼀个MySQL数据的连接,在connect中可以接受以下参数
host=None, user=None, password="",
database=None, port=0, unix_socket=None,
charset='', sql_mode=None,
read_default_file=None, conv=None, use_unicode=None,
client_flag=0, cursorclass=Cursor, init_command=None,
connect_timeout=10, ssl=None, read_default_group=None,
compress=None, named_pipe=None, no_delay=None,
autocommit=False, db=None, passwd=None, local_infile=False,
max_allowed_packet=16*1024*1024, defer_connect=False,
auth_plugin_map={}, read_timeout=None, write_timeout=None,
bind_address=None
host表⽰主机名;user表⽰⽤户名;password表⽰密码;
database表⽰指定的数据库;port表⽰端⼝号,默认3306;
charset表⽰指定字符编码(utf8mb4相当于utf-8)
②连接到了数据库之后,就是对数据库进⾏增删改:
⾸先要定义⼀个游标cursor,通过游标来操作数据库,别忘了操作完后要关闭游标
cursor = db.cursor()
sql = 'select * from beauty' # 要执⾏的sql语句
# 执⾏sql语句
dbmit() # 若操作数增删改则需要提交数据
若为查询语句,得到数据后要打印数据需要⽤到fetchall函数
cursor = db.cursor()
sql = 'select * from beauty' # 要执⾏的sql语句
# 执⾏sql语句
# 将执⾏的结果传递给data,为字符串形式
data = cursor.fetchall()
for row in data:
print(row)
结果:
若要知道数据对应的是哪列可以⽤进阶的⽅法:
cursor = db.cursor()
sql = 'select * from beauty' # 要执⾏的sql语句
# 默认情况下,我们获取到的返回值是元组,只能看到每⾏的数据,
# 却不知道每⼀列代表的是什么,这个时候可以使⽤以下⽅式来返回字典,每⼀⾏的数据都会⽣成⼀个字典:# 在实例化的时候,将属性cursor设置为pymysql.cursors.DictCursor
cursor = db.cursor(cursor=pymysql.cursors.DictCursor)
data = cursor.fetchall()
for row in data:
print(row)
结果:
③操作完数据库要关闭资源
# 关闭游标
cursor.close()
# 关闭数据库
db.close()
④标准格式
import pymysql
DATABASE = {
'host': 'localhost',
'database': 'girls',
'user': 'root',
'password': '000000'
}
conn = t(**DATABASE) my_cursor = None
数据库恢复try:
16号线地铁线路图my_cursor = conn.cursor()
sql = "select * from beauty"
ute(sql)
result = my_cursor.fetchall()
for row in result:
print(row)
except Exception as e:
print(e)
finally:
if my_cursor is not None:
try:
my_cursor.close() # 关闭游标
except Exception as e:
print(e)
try:
connmit() # 提交数据
conn.close() # 关闭数据库
中秋团圆诗句except Exception as e:
print(e)
⼆、⽂件的读取
粗略来说⽂件可以分为3种:⽂本⽂件、⼆进制⽂件和⼤⽂件
python操作⽂件是⽤open函数,其打开试试默认以⽂本⽂件形式打开的但是open函数默认编码是None,所以处理⽂本⽂件时,要指定其⽂件编码
①读取⼩⽂件
file_name = ''
try:
with open(file_name, encoding='utf-8') as file_obj:
content = ad(6)
content = ad(6)
content = ad(6)
content = ad(6)
except FileNotFoundError :
print(f'{file_name} 这个⽂件不存在!')
read的⼏点注意事项:
通过read来读取⽂件,但直接调⽤read()它会将⽂本⽂件的所有内容全部都读取出来,所以如果要读取的⽂件较⼤的话,会⼀次性将⽂件的内容加载到内存中,容易导致内存泄漏,所以对于较⼤的⽂件,不要直接调⽤read()。
要打印数据时,read()可以接收⼀个size作为参数,该参数⽤来指定要读取的字符的数量,默认值为-1,它会读取⽂件中的所有字符,每⼀次读取都是从上次读取到位置开始读取的,如果字符的数量⼩于size,则会读取剩余所有的,如果已经读取到了⽂件的最后了,则会返回''空串
结果:
②如果要读取⼤⽂件
try:
with open(file_name,encoding='utf-8') as file_obj:
# 定义⼀个变量,来保存⽂件的内容
file_content = ''
# 定义⼀个变量,来指定每次读取的⼤⼩
chunk = 100
# 创建⼀个循环来读取⽂件内容
while True:
# 读取chunk⼤⼩的内容
content = ad(chunk)
# 检查是否读取到了内容
if not content:
娇组词是# 内容读取完毕,退出循环
break
# 输出内容
# print(content,end='')
file_content += content
except FileNotFoundError :
print(f'{file_name} 这个⽂件不存在!')
③readline与readlines杜甫诗全集
readline():该⽅法可以⽤来读取⼀⾏内容
readlines():该⽅法⽤于⼀⾏⼀⾏的读取内容,它会⼀次性将读取到的内容封装到⼀个列表中返回file_name = ''
with open(file_name, encoding='utf-8') as file_obj:
print(adline(), end='')
r = adlines()
pprint.pprint(r[0])
且注意:readline读过的数据readlines不会再读了,反过来也是⼀样
结果:
母亲节给妈妈发多少红包合适
三、⼀些可能⽤到的类
①time
import time
# ⼀、时间获取:

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。