简体   繁体   中英

How to make an SQL file readable with python?

The question may be a bit broad but I will try and sum up my problem. I have an SQL file that when opened begins like so:

-- MySQL dump 10.13  Distrib 5.7.13, for Linux (x86_64)
--
-- Host: localhost    Database: northwind
-- ------------------------------------------------------
-- Server version   5.7.13-0ubuntu0.16.04.2

This heading shows that this sql file is written and meant for Linux I'm guessing which may be an important factor but I am currently clueless. The assignment that I am doing asks me to create an application using any programming language of my choosing to read a database and give the user options like add a customer, remove an order, 'ship' an order etc. Since python is the language I usually work with the most and am most fluent in I would like to program using python. However I cannot seem to find a way in code to read this SQL file, create a DB an work through it. I have tried using sqlite3 but always end up running into errors when trying to execute or read through the file. Some attempts look like:

import sqlite3
from sqlite3 import OperationalError

conn = sqlite3.connect(":memory:")
c = conn.cursor()

fd = open('northwind2.sql','r')
sqlFile = fd.read()
fd.close()

sqlCommands = sqlFile.split(';')

for command in sqlCommands:
    try:
        c.execute(command)
    except OperationalError, msg:
        print("Command Skipped:", msg)

c.close()
conn.close()

Which gives me a syntax error

File "imtrying.py", line 16
    except OperationalError, msg:
                           ^
SyntaxError: invalid syntax

This may be because I have typed something incorrectly but I found an example almost picture perfect to this one on here and they did not have an error. If I remove the msg I do get a result of multiple lines of the printed statement "Command Skipped:". After continuing on with this attempt, I found that some of the lines that are skipped are crucial to creating the database for instance the line:

--
-- Current Database: `northwind`
--

CREATE DATABASE /*!32312 IF NOT EXISTS*/ `northwind` /*!40100 DEFAULT CHARACTER SET latin1 */;

This is the first line that is skipped when trying to exectute. Another attempt looks like:

import sqlite3

connection = sqlite3.connect(":memory:")

cursor = connection.cursor()

sql_file = open("northwind.sql")
sql_as_string = sql_file.read()
cursor.executescript(sql_as_string)

connection.commit()

connection.close()

This actually gets into the sql but gives me the error:

Traceback (most recent call last):
  File "imtrying.py", line 9, in <module>
    cursor.executescript(sql_as_string)
sqlite3.OperationalError: near "DATABASE": syntax error

I'm hoping this is something I am just typing incorrectly but both of these examples are my versions of others online. Any tips or questions for more information would be greatly appreciated.

except OperationalError, msg: 

is not correct python syntax, try

except OperationalError as msg:

Your file is a Mysqldump file which works with mysql-server if you want to migrate a Mysqldump file to sqlite have a look at https://gist.github.com/esperlu/943776#file-mysql2sqlite-sh . If you want to use mysql then you need to install it first or in replace use MariaDb, for python you can use https://github.com/mysql/mysql-connector-python .

This heading shows that this sql file is written and meant for Linux I'm guessing which may be an important factor but I am currently clueless.

For mysql-server running on Linux to be exactly Ubuntu/Linux mysql-server version 5.7.13-0ubuntu0.16.04.2, it means the file was generated using ubuntu with that mysql server version.

conn = sqlite3.connect(":memory:")

This line creates the DB and it's doing the same as

CREATE DATABASE / !32312 IF NOT EXISTS / northwind /*;40100 DEFAULT CHARACTER SET latin1 */;

Note here sqlite3 uses a different sintax than mysql, so if you want to create a DB using sqlite3 the only thing you need to do is connect to it, so in Python it will be something like

import sqlite3 
conn = sqlite3.connect("/path/northwind.db")

While in mysql the command for creating a DB is CREATE DATABASE northwind; In this case you need to recover your Database first, after mysql-server and mysql-client open your terminal and type

mysql -u YOUR_USER -p
source /your_path_to_dump_file.sql

After that you can connect to Mysql using python

conn = mysql.connector.connect(
    host="localhost",
    port=3306,
    user="YOUR_USER",
    password="YOUR_PASSWORD")
c = conn.cursor()

After that you can make transactions to your db, also you can source your file from python, using your cursor so it could be:

c.execute("source /path_to_your_file.sql")
c.commit()

NOTE: code untested just for reference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM