在python中将excel数据发布到mysqldb
Posted
技术标签:
【中文标题】在python中将excel数据发布到mysqldb【英文标题】:post excel data into mysqldb in python 【发布时间】:2020-07-01 23:12:37 【问题描述】:将数据插入 mysql 数据库时,抛出“MySQLdb._exceptions.OperationalError: (1046, 'No database selected')”。我已在此代码 sn-p 中逐步检查。数据库已连接。但它显示上述错误。
这是我创建的数据库查询。
create table database_conn(id int,name varchar(20),age int(100),address varchar(50),state varchar(10), zipcode int(20));
这是我的 python 代码 sn-p 用于将数据插入数据库。
class Database_conn:
def __init__(self,host,user,password,database,file_name):
self.host = host
self.user = user
self.password = password
self.database = database
self.file_name = file_name
self.conn()
self.excel_sheet()
self.db_query()
def conn(self):
self.mydb = MySQLdb.connect(host=self.host,
user = self.user,password = self.password)
def excel_sheet(self):
self.book = xlrd.open_workbook(self.file_name)
self.sheet = self.book.sheet_by_name("Sheet1")
try:
self.cursor = self.mydb.cursor()
print('established cursor connection')
except:
print('something goes worng')
def db_query(self):
global values, conn
self.query = """INSERT INTO database_conn(id,name,age,address,state,zipcode)VALUES(%s,%s,%s,%s,%s,%s)"""
for i in range(1,self.sheet.nrows):
id = self.sheet.cell(i,0).value
name = self.sheet.cell(i,1).value
age = self.sheet.cell(i,2).value
address = self.sheet.cell(i,3).value
state = self.sheet.cell(i,4).value
zipcode = self.sheet.cell(i,5).value
values = (id, name, age, address, state, zipcode)
conn = self.cursor
conn.execute(self.query, values)
conn.commit()
conn.close()
file_name = input('Enter a file_name : ')
d=Database_conn('localhost','****','****','****',file_name)
在上面的代码中,我在这一行得到了错误
conn.execute(self.query, values)
【问题讨论】:
【参考方案1】:最后我确实将数据插入到我的数据库中。
这是我的答案:
class Database_conn:
def __init__(self,host,user,password,database,file_name):
self.host = host
self.user = user
self.password = password
self.database = database
self.file_name = file_name
self.conn()
self.excel_sheet()
self.db_query()
self.db_close()
def conn(self):
self.mydb = MySQLdb.connect(host=self.host,
user = self.user,password = self.password,db=self.database)
def excel_sheet(self):
self.book = xlrd.open_workbook(self.file_name)
self.sheet = self.book.sheet_by_name("Sheet1")
try:
self.cursor = self.mydb.cursor()
print('established cursor connection')
except:
print('something goes worng')
def db_query(self):
global values, conn
self.query = """INSERT INTO database_conn(id,name,age,address,state,zipcode)VALUES(%s,%s,%s,%s,%s,%s)"""
for i in range(1,self.sheet.nrows):
id = self.sheet.cell(i,0).value
name = self.sheet.cell(i,1).value
age = self.sheet.cell(i,2).value
address = self.sheet.cell(i,3).value
state = self.sheet.cell(i,4).value
zipcode = self.sheet.cell(i,5).value
values = (id, name, age, address, state, zipcode)
conn = self.cursor
conn.execute(self.query, values)
conn.close()
def db_close(self):
self.database.commit()
self.database.commit()
file_name = input('Enter a file_name : ')
d=Database_conn('localhost','****','****','*****',file_name)
【讨论】:
以上是关于在python中将excel数据发布到mysqldb的主要内容,如果未能解决你的问题,请参考以下文章
如何在 Python 中将 Azure Blob 文件 CSV 转换为 Excel
如何在从 python 触发的电子邮件中将 pandas 数据框附加为 excel