简体   繁体   中英

Unable to add list of list in mysql database

I have a dictonary whose format is as follows:

{"student_name" : ["stream", [["Subject", "Lesson", "Problem", "need_help?"]]]} 

It has the key as the name of the student, and the value mapped to that key is a list which contains, at index[0] , A string value of stream, and on index[1] is a list of list . Here's an example of an actual entry:

  {"Aron" : ["Science", [["Physics", "Waves", "Simple harmonic Motion", True]
                        ["Maths", "InverseTrig function, 3rd excercuse.", True]
                        ["History", "Renissance", "Leo Vinci's contrubution"]], 
"Timmy" : ["Applied Science", ["Computer", "Recursion", False],
...

I have no control over the list of list . I used to convert everything to json and and then store the json in file/s. But it was messy, and so i decided to store everything in a MySQL database.

I am using this script

        def insert(self, database, table, QUERY):
            db = msd.connect(self.host,self.user,self.password, database )
            cursor = db.cursor()

            try:
                cursor.execute(QUERY)
               db.commit()
            except:
                print name
            db.close()

      def push(self, name, stream, taken_subjects, section):
        QUERY = 'INSERT INTO subject (Name, Class, Section, taken_subjects) VALUES ("{}", "{}", "{}", "{}")'.format(name, stream, section, taken_subjects)
        insert(database, table_name, QUERY)

And this script to transfer the data, one at a time from json to db:

files = open("Student_dict_json", "rU")
jsondb = json.loads(files.read())

student_name = jsondb.keys()

db = transfer_to_db() #Name of the class above
counter = 0
for name in student_name:
    db.push(name, jsondb[name][0], jsondb[name][1], counter)
    counter = counter+1

but i am getting this error:

_mysql_exceptions.ProgrammingError: (1064, 'You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near \'Get Shorty" but I can\\\'t remember the page right now.\', u\'high\', False]]")\' at line 1')

I have officially given up trying to debug this!

Also, i am converting list to a string and then storing that string in the MySQL database table(TEXT). But idk if it's the best way to do it, as if a student, say Aron decides that he has a problem in literature also then i'll have to pull the list(stored as string) and then convert it into list and then convert it back again to string and then first delete the string that was there, and update it with a new one. Is there a better way to do it?

PS: Here's a dummy snapshot of the table in case my explanation was confusing.

+---------------+---------+---------+------------------------------------------------------------------------------------------+
| Name          | Stream  | Index   | taken_subjects                                                                           |
+---------------+---------+---------+------------------------------------------------------------------------------------------+
| Drake         | History |       8 | [['French revolution', 'was it avoidable?', True]]                                       |
| Tommy Simmons | Science |       9 | [['Physics', 'Vector', 'Triangle Law', True],['Literature', 'Macbeth', 'Act-II', False]] |
+---------------+---------+---------+------------------------------------------------------------------------------------------+

Please, any help will be appreciated.

I was able to debug it by using json.dumps() .

 def push(self, name, stream, taken_subjects, section):
        QUERY = 'INSERT INTO subject (Name, Class, Section, taken_subjects) VALUES ("{}", "{}", "{}", "{}")'.format(name, stream, section,  json.dumps(taken_subjects))
        insert(database, table_name, QUERY)

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