mysql 命令不同步_MySQL命令不同步Python

我试图用MySQL和pythonmysql连接器初始化我的数据库。

运行下面的代码后,我收到以下异常:mysql.connector.errors.DatabaseError: 2014 (HY000): Commands out of sync; you can't run this command now

我尝试了几件事,包括提交连接对象,但没有什么对我有效。在def initialization():

cnx = mysql.connector.connect(user="root", password="xxxxxxxxx", host='127.0.0.1')

cursor = cnx.cursor()

cursor.execute("CREATE DATABASE IF NOT EXISTS izugitdb;")

cursor.execute("USE izugitdb;")

cursor.execute("CREATE TABLE IF NOT EXISTS employee_users (user_id INT AUTO_INCREMENT PRIMARY KEY,username VARCHAR(40) NOT NULL, password VARCHAR(40) NOT NULL,isadmin TINYINT(1) DEFAULT 0 NOT NULL);")

cursor.execute("CREATE TABLE IF NOT EXISTS clients (client_id INT AUTO_INCREMENT PRIMARY KEY,client_name VARCHAR(255) NOT NULL); ENGINE = InnoDB;")

cursor.execute("CREATE TABLE IF NOT EXISTS clients_workers (id INT AUTO_INCREMENT PRIMARY KEY, worker_id INT ,worker_name VARCHAR(255) NOT NULL, client_id INT);")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python通过使用`elasticsearch`库和`mysql-connector-python`库可以实现MySQL数据库同步到Elasticsearch的功能。下面是一个300字的回答。 首先,安装所需的库。可以使用以下命令安装`elasticsearch`库和`mysql-connector-python`库: ``` pip install elasticsearch mysql-connector-python ``` 接下来,导入所需的库并连接到MySQL数据库和Elasticsearch: ```python import mysql.connector from elasticsearch import Elasticsearch # 连接到MySQL数据库 conn = mysql.connector.connect( host="localhost", user="root", password="password", database="mydatabase" ) # 连接到Elasticsearch es = Elasticsearch([{'host': 'localhost', 'port': 9200}]) ``` 然后,执行MySQL查询语句来获取数据,并将其插入到Elasticsearch中: ```python # 创建MySQL游标对象 cursor = conn.cursor() # 执行MySQL查询语句 cursor.execute("SELECT * FROM mytable") # 获取查询结果 results = cursor.fetchall() # 将结果插入到Elasticsearch for row in results: document = { 'id': row[0], # 假设MySQL表中有一个id列 'name': row[1], # 假设MySQL表中有一个name列 # 添加其他需要同步的字段 } es.index(index='myindex', doc_type='mytype', body=document) ``` 最后,关闭MySQL数据库连接和Elasticsearch连接: ```python # 关闭MySQL数据库连接 conn.close() # 关闭Elasticsearch连接 es.close() ``` 以上是用Python实现MySQL数据库同步到Elasticsearch的基本步骤。可以根据具体需求对代码进行更改和优化,例如使用配置文件来管理数据库连接信息和Elasticsearch的索引名称等。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值