2017-06-20 3 views
1

Gibt es eine Möglichkeit, JSON-, XML- oder CSV-Daten an einen lokalen MySQL-Server zu senden?JSON- oder XML- oder CSV-Daten zum MySQL-Server hinzufügen

Ich bin neu in MySQL und konnte nichts online finden.

Entweder funktioniert der Datentyp, da ich über Code verfüge, der alle meine Daten in das von mir benötigte Format konvertieren kann, z. B. JSON, XML und CSV.

Jede Hilfe wird geschätzt!

Antwort

3

1). Ich gebe Ihnen die Antwort für JSON >>Wie speichert man JSON-Daten in MySQL DB mit Python?

Wenn Ihr JSON-Format folgt und Sie möchten in MySQL assoziativ speichern database >> table dann können Sie das erste Beispiel folgen.

Beispiel: 1

JSON-Format

{ 
"first_key" : 10, 
"second_key" : 20 
} 

Python Core-Skript für JSON.

import MySQLdb 

myjson = json.loads(jdata) 

def dbconnect(): 
    try: 
     db = MySQLdb.connect(
      host='localhost', 
      user='root', 
      passwd='', 
      db='myjson_db' 
     ) 
    except Exception as e: 
     sys.exit("Can't connect to database") 
    return db 

cursor = db.cursor() 

sql = """INSERT INTO my_table (array_key, array_value) VALUES (%s, %s)""" 

for array_key, array_value in myjson.items(): 
    cursor.execute(sql, (array_key, array_value)) 

Wenn Sie Daten in nur einer Spalte speichern möchten, folgen Sie der zweiten wie folgt.

Beispiel: 2

import MySQLdb 

myjson = json.loads(jdata) 

def dbconnect(): 
    try: 
     db = MySQLdb.connect(
      host='localhost', 
      user='root', 
      passwd='', 
      db='myjson_db' 
     ) 
    except Exception as e: 
     sys.exit("Can't connect to database") 
    return db 

cursor = db.cursor() 

sql = """INSERT INTO my_table (json_column) VALUES (%s)""" 

cursor.execute(sql, (myjson)) 

2). Beginnen wir mit XML >>Wie speichert man XML-Daten in MySQL DB mit Python?

XML-Daten

<?xml version="1.0" encoding="UTF-8" ?> 
<first_key>10</first_key> 
<second_key>20</second_key> 

Der nächste Schritt ist: bitte installieren: Python-Skript für Konvertiten XML zu JSON aus here Import und import xml2json in unserem Python Core-Skript.

Python Core-Skript für XML

import MySQLdb 
import xml2json 
import json 

xml_data = json.loads(xml2json.xml2json(xmldata)) 

### data store functionality or logic is same as example 1 and example 2 
def dbconnect(): 
    try: 
     db = MySQLdb.connect(
      host='localhost', 
      user='root', 
      passwd='', 
      db='myxml_db' 
     ) 
    except Exception as e: 
     sys.exit("Can't connect to database") 
    return db 

cursor = db.cursor() 

sql = """INSERT INTO my_table (xml_data) VALUES (%s)""" 

cursor.execute(sql, (xml_data)) 

3). Lets diskutieren für CSV >>Wie CSV-Daten in MySQL DB mit Python speichern?

import csv 
import MySQLdb 

csv_data = csv.reader(file('my_csv_file.csv')) 

def dbconnect(): 
    try: 
     db = MySQLdb.connect(
      host='localhost', 
      user='root', 
      passwd='', 
      db='mycsv_db' 
     ) 
    except Exception as e: 
     sys.exit("Can't connect to database") 
    return db 

for row in csv_data: 

    cursor.execute('INSERT INTO my_csv_table(csv_first_column, \ 
      csv_first_column)' \ 
      'VALUES("%s", "%s")', 
      row) 
0

Ich bin nicht bewusst sowieso JSON einzufügen, XML oder CSV in eine MySQL-Datenbank direkt.

Sie können die Daten zu einem Skript analysieren, das sie unter Verwendung eines Moduls wie MySQL-python in eine Datenbank einfügen kann.

Mein Python ist nicht großartig, aber hoffentlich sollte dieses Beispiel ausreichen.

#!usr/bin/python 
# Import mySQL module to interact with database. 
import MySQLdb 
# Import json module to convert the JSON into a Python data structure. 
import json 

# Convert the JSON to a usable format. 
data = json.loads(yourjson) 

# Connect to MySQL server. 
db = mySQLdb.connect(host='yourhost', 
        user='youruser', 
        passwd='yourpassword', 
        db='yourschema') 

# Create an object to handle SQL statements. 
cur = db.cursor() 

# Attempt to execute the SQL statement, if not revert any changes. 
try: 
    cur.execute('INSERT INTO table SET col1 = %s, col2 = %s', data.foo, data.bar) 
    db.commit() 
except: 
    db.rollback() 
Verwandte Themen