简体   繁体   English

无法在mysql数据库中添加列表列表

[英]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 . 它具有作为学生姓名的关键字,并且映射到该关键字的值是一个列表 ,该列表index[0]处包含流的字符串值,在index[1]上包含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 . 我无法控制list的列表 I used to convert everything to json and and then store the json in file/s. 我曾经将所有内容都转换为json,然后将json存储在文件中。 But it was messy, and so i decided to store everything in a MySQL database. 但这很麻烦,所以我决定将所有内容存储在MySQL数据库中。

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: 这个脚本可以一次将数据从json传输到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). 另外,我将列表转换为字符串,然后将该字符串存储在MySQL数据库表(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. 但是,如果这是最好的方法,那么idk是最好的方法,就像一个学生说的那样,阿隆(Aron)认为他在文学方面也有问题,那么我将不得不拉列表(存储为字符串),然后将其转换为列表,然后进行转换再次将其返回到字符串,然后首先删除那里的字符串,并用新的字符串进行更新。 Is there a better way to do it? 有更好的方法吗?

PS: Here's a dummy snapshot of the table in case my explanation was confusing. PS:这是该表的虚拟快照,以防我的解释造成混淆。

+---------------+---------+---------+------------------------------------------------------------------------------------------+
| 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() . 我可以使用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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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