mysql-python

Use of '.format()' vs. '%s' in cursor.execute() for mysql JSON field, with Python mysql.connector,

那年仲夏 提交于 2021-02-18 16:59:53
问题 My objective is to store a JSON object into a MySQL database field of type json, using the mysql.connector library. import mysql.connector import json jsonData = json.dumps(origin_of_jsonData) cnx = mysql.connector.connect(**config_defined_elsewhere) cursor = cnx.cursor() cursor.execute('CREATE DATABASE dataBase') cnx.database = 'dataBase' cursor = cnx.cursor() cursor.execute('CREATE TABLE table (id_field INT NOT NULL, json_data_field JSON NOT NULL, PRIMARY KEY (id_field))') Now, the code

Stored procedure works on MySQL workbench, but not in python

点点圈 提交于 2021-02-10 16:21:52
问题 I'm testing a database where i have 3 tables, number 1 is storing user information, number 2 is storing the latitude and longitude for different cities, and there's a 3rd table storing the location of a user and it's distance to the closest city. The 3rd table has a composite key from primary keys of the other two tables. I have written a stored procedure in mysql workbench that takes in 5 variables then it inserts data into the 3rd table, here's the code using some random variables: SET

Stored procedure works on MySQL workbench, but not in python

给你一囗甜甜゛ 提交于 2021-02-10 16:21:19
问题 I'm testing a database where i have 3 tables, number 1 is storing user information, number 2 is storing the latitude and longitude for different cities, and there's a 3rd table storing the location of a user and it's distance to the closest city. The 3rd table has a composite key from primary keys of the other two tables. I have written a stored procedure in mysql workbench that takes in 5 variables then it inserts data into the 3rd table, here's the code using some random variables: SET

Executing different queries using mysql-python

雨燕双飞 提交于 2021-02-09 04:16:16
问题 I'm working with a remote db for importing data to my Django proyect's db. With the help of MySQLdb I've managed with ease to create an importing function like the following: def connect_and_get_data(useful_string): CONNECTION = MySQLdb.connect(host=..., port=..., user=..., passwd=..., db=..., cursorclass=MySQLdb.cursors.DictCursor, charset = "utf8") cursor = CONNECTION.cursor() cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", (useful_string)) result = cursor.fetchall()

Executing different queries using mysql-python

痴心易碎 提交于 2021-02-09 04:03:08
问题 I'm working with a remote db for importing data to my Django proyect's db. With the help of MySQLdb I've managed with ease to create an importing function like the following: def connect_and_get_data(useful_string): CONNECTION = MySQLdb.connect(host=..., port=..., user=..., passwd=..., db=..., cursorclass=MySQLdb.cursors.DictCursor, charset = "utf8") cursor = CONNECTION.cursor() cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", (useful_string)) result = cursor.fetchall()

Executing different queries using mysql-python

ⅰ亾dé卋堺 提交于 2021-02-09 04:00:23
问题 I'm working with a remote db for importing data to my Django proyect's db. With the help of MySQLdb I've managed with ease to create an importing function like the following: def connect_and_get_data(useful_string): CONNECTION = MySQLdb.connect(host=..., port=..., user=..., passwd=..., db=..., cursorclass=MySQLdb.cursors.DictCursor, charset = "utf8") cursor = CONNECTION.cursor() cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", (useful_string)) result = cursor.fetchall()

Executing different queries using mysql-python

大憨熊 提交于 2021-02-09 03:59:27
问题 I'm working with a remote db for importing data to my Django proyect's db. With the help of MySQLdb I've managed with ease to create an importing function like the following: def connect_and_get_data(useful_string): CONNECTION = MySQLdb.connect(host=..., port=..., user=..., passwd=..., db=..., cursorclass=MySQLdb.cursors.DictCursor, charset = "utf8") cursor = CONNECTION.cursor() cursor.execute("SELECT ... FROM ... WHERE ... AND some_field=%s", (useful_string)) result = cursor.fetchall()

How can I update a timestamp field in MySQL through Python?

故事扮演 提交于 2021-01-29 08:01:34
问题 I have database table with the following schema, schema hash VARCHAR(32) NOT NULL, regional_unit VARCHAR(64) NOT NULL, url VARCHAR(2048) NOT NULL, created_at TIMESTAMP NOT NULL DEFAULT '0000-00-00 00:00:00', updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW(), PRIMARY KEY (hash) that I update with the following statement through Python: query INSERT INTO urls (hash, regional_unit, url, created_at, updated_at) VALUES (%s, %s, %s, %s, %s) ON DUPLICATE KEY UPDATE hash=hash; [['...', '...

Using MysqlDb in python converts null to None

♀尐吖头ヾ 提交于 2021-01-28 22:13:50
问题 Below is my code import MysqlDB as mob Mysqlconn = mdb.connect(hostname, username, password, databasename,port=3306,cursorclass=mdb.cursors.DictCursor) Mysqlcur = self.Mysqlcon.cursor() Mysqlcur.execute("Select * from users where date = %(date_check)s,{"date_check":current_date}) row = self.Mysqlcur.fetchall() fileOpen = open("filename.csv","w") for each in row: fileOpen.write(str(each["A"])+","+str(each["B"])) It works fine for the rows which does not have null values. When it encounters a

Executing multiple MySQL inserts at once in Python

一世执手 提交于 2021-01-28 05:18:15
问题 I have a string that is basically a concatenation of multiple insert statements such as sql = INSERT INTO test (a,b,c) VALUES ("test","test",1);INSERT INTO test (a,b,c) VALUES ("2nd test","2nd test",6); When I run this in SQL as a query, it works fine and inserts both statements. However, when I run it in python using the following: cursor = db.cursor() sql = INSERT INTO test (a,b,c) VALUES ("test","test",1);INSERT INTO test (a,b,c) VALUES ("2nd test","2nd test",6); cursor.execute(sql) db