Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python从excel创建sqlite3数据库;但是,它会将数据库名称创建为;database.xlsx.db_Python_Pandas_Sqlite - Fatal编程技术网

Python从excel创建sqlite3数据库;但是,它会将数据库名称创建为;database.xlsx.db

Python从excel创建sqlite3数据库;但是,它会将数据库名称创建为;database.xlsx.db,python,pandas,sqlite,Python,Pandas,Sqlite,嗨, 以上是当我点击“上传花名册”按钮(tk按钮)时调用的函数。将数据完美地更新到sqlite3数据库中;但是,问题是它将数据库文件创建为“nameofdatabase.xlsl.db”,而不是“nameofdatabase.db”。另外,由于数据库中有一个表,所以默认情况下,它将表名设置为“0”(零)。 有什么建议吗?使用os.path.splitext: excel_file = self.filename con = sqlite3.connect(excel_file + ".db")

嗨, 以上是当我点击“上传花名册”按钮(tk按钮)时调用的函数。将数据完美地更新到sqlite3数据库中;但是,问题是它将数据库文件创建为“nameofdatabase.xlsl.db”,而不是“nameofdatabase.db”。另外,由于数据库中有一个表,所以默认情况下,它将表名设置为“0”(零)。
有什么建议吗?

使用
os.path.splitext

excel_file = self.filename
con = sqlite3.connect(excel_file + ".db")
wb = pd.read_excel(excel_file, sheet_name = [0])
for sheet in wb:
    wb[sheet].to_sql(sheet, con, index=False)
con.commit()
con.close()

请阅读“nameofdatabase.xlsl.db”作为“nameofdatabase.xlsx.db”,谢谢您的作品。。。。。。。你能帮我做点别的吗?上面的代码总是覆盖现有的数据库,是否有任何方法附加数据库?其次,上面的代码创建的db生成表,其名称为0(零)。我们可以将表重命名为其他名称吗?您是否阅读了
to_sql
的文档?您希望传递的第一个参数不是
sheet
,而是
,如果\u exists='append'
,则使用
import os
con = sqlite3.connect(os.path.splitext(excel_file)[0] + ".db")