替换mysql connector,用变量替换表名。使用python和mysql连接器

I would like to dynamically change the variable name of the table I insert data into.

This currently works,

def dataEntry(subreddit, _title, _post_url, _imageURL):

cnx = mysql.connector.connect(**config)

c = cnx.cursor()

insert = ("""INSERT INTO FoodPorn

(subreddit, title, post_url, imageURL)

VALUES (%s, %s, %s, %s)""")

data_value = (subreddit, _title, _post_url, _imageURL)

c.execute(insert, data_value)

cnx.commit()

c.close()

cnx.close()

dataEntry("fake", "fake", "fake", "fake")

but when I try and do the same for the table name in this case "FoodPorn", but for a dynamic one such as in this example MachinePorn,

def dataEntry(subreddit, _title, _post_url, _imageURL):

cnx = mysql.connector.connect(**config)

c = cnx.cursor()

insert = ("""INSERT INTO subredditName

(subreddit, title, post_url, imageURL)

VALUES (%s, %s, %s, %s, %s)""")

data_value = ("MachinePorn", subreddit, _title, _post_url, _imageURL)

c.execute(insert, data_value)

cnx.commit()

c.close()

cnx.close()

dataEntry("fake", "fake", "fake", "fake")

I get this error,

mysql.connector.errors.ProgrammingError: 1146 (42S02): Table 'sytykr.subredditname' doesn't exist

This leads me to believe I cannot do it this way and so I would like to ask How can I do it so I can eventually pass a variable name in of the Table, instead of having to hard code it each time.

解决方案

The exception that is showing mysql connector is telling you that the table doesn't exist in your database.

In addition, you're trying to use 'MachinePorn' as the argument but you didn't define that in the query, it's hardcoded 'subredditName'.

I think you should define database as another parameter in the query and it will run fine:

def dataEntry(subreddit, _title, _post_url, _imageURL):

cnx = mysql.connector.connect(**config)

c = cnx.cursor()

insert = cnx.escape_string("INSERT INTO MachinePorn (subreddit, title, post_url, imageURL) VALUES (%s, %s, %s, %s)")

data_value = (subreddit, _title, _post_url, _imageURL)

c.execute(insert, data_value)

cnx.commit()

c.close()

cnx.close()

dataEntry("fake", "fake", "fake", "fake")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
抱歉,我不能为您提供完整的源代码,因为这需要涉及到很多方面,而且需要根据您的具体需求和数据库架构来编写。但是,我可以给您提供一些参考,以帮助您开始编写图书馆管理系统。 以下是一些基本的PythonMySQL代码示例,用于连接和操作数据库: ```python # 导入MySQL连接器库 import mysql.connector # 连接到MySQL数据库 mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) # 创建游标对象 mycursor = mydb.cursor() # 执行SELECT查询 mycursor.execute("SELECT * FROM books") # 获取查询结果 result = mycursor.fetchall() # 打印结果 for x in result: print(x) ``` 在以上示例中,我们使用PythonMySQL连接器库来连接到MySQL数据库,并执行了一个SELECT查询。该查询检索books表中的所有行,并将它们作为Python元组返回。我们使用for循环来迭代结果,并将每个元组打印到控制台。 接下来,我们可以编写一个基本的Web应用程序,其中包括视图和模板来显示图书馆的书籍和读者信息。以下是一个使用Flask框架的Python代码示例: ```python # 导入Flask框架 from flask import Flask, render_template import mysql.connector # 创建Flask应用程序对象 app = Flask(__name__) # 定义路由和视图 @app.route("/") def index(): # 连接到MySQL数据库 mydb = mysql.connector.connect( host="localhost", user="yourusername", password="yourpassword", database="yourdatabase" ) # 创建游标对象 mycursor = mydb.cursor() # 执行SELECT查询 mycursor.execute("SELECT * FROM books") # 获取查询结果 books = mycursor.fetchall() # 关闭数据库连接 mydb.close() # 渲染模板并返回结果 return render_template("index.html", books=books) # 启动应用程序 if __name__ == "__main__": app.run() ``` 在以上示例中,我们使用Flask框架创建了一个基本的Web应用程序。我们定义了一个路由和视图函数来处理网站首页。在视图函数中,我们连接到MySQL数据库并执行一个SELECT查询,检索books表中的所有行。我们将查询结果存储在books变量中,并将其传递到渲染index.html模板的render_template函数中。 在模板中,我们可以使用Flask自带的Jinja2模板语言来渲染数据库中的数据。以下是一个示例index.html模板: ```html <!DOCTYPE html> <html> <head> <title>图书馆管理系统</title> </head> <body> <h1>图书馆书籍列表</h1> <table> <tr> <th>书籍编号</th> <th>书名</th> <th>作者</th> <th>出版社</th> </tr> {% for book in books %} <tr> <td>{{ book[0] }}</td> <td>{{ book[1] }}</td> <td>{{ book[2] }}</td> <td>{{ book[3] }}</td> </tr> {% endfor %} </table> </body> </html> ``` 在以上示例中,我们使用了Jinja2模板语言来循环books变量中的数据,并将它们渲染成一个HTML表格。每个书籍都被渲染为一行,其中包括其书籍编号、书名、作者和出版社。 希望这些示例可以帮助您开始编写自己的图书馆管理系统!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值