Python连接操作Mysql数据库的方法

我们直接得出结论: 1 .安装PyMySQL:pip install pymysql。
2 .连接MySQL: 蟒蛇 导入pymysql conn = pymysql.connect(主机='1 2 7 .0.0.1 ',用户='root',pass='1 2 3 4 5 6 ',db='test_db') 3 .执行查询: 蟒蛇 光标 = conn.cursor() 光标.execute("从用户中选择") 数据 = 游标.fetchall() 打印(数据) 光标.close() conn.close() 4 、交易处理: 蟒蛇 尝试: cursor.execute("更新帐户设置余额=-1 000 WHERE user_id=1 ") cursor.execute("更新帐户设置余额=+1 000 WHERE user_id=2 ") conn.commit() 除外: conn.rollback() 5 、注意事项:
使用DictCursor返回字典:Cursorclass=pymysql.cursors.DictCursor。

关闭连接:最终:cursor.close() conn.close()。

防注入:cursor.execute("SELECT FROM users WHERE id=%s", (user_id,)).
批量操作:cursor.executemany("INSERT INTO user VALUES (%s, %s), data)。

别浪费我的时间了,用它吧。

如何用python连接 tableau 数据库,然后读取数据

这是一个陷阱。
直接串联 SQL 查询可能会导致字段引用错误,例如 au_id 字段在两个表中重复出现。

不信,不要直接使用带有重复字段的SQL查询。

不要这样做,当使用 Tableau 绑定到 SQL 查询时,请确保字段在所有引用的表中是唯一的。