无法在mysql数据库

时间:2015-12-05 14:22:22

标签: python mysql json

我有一个dictonary,其格式如下:

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

它有一个键作为学生的名称,映射到该键的值是列表,其中包含index[0],一个字符串流的值,index[1]上的 列表 。 以下是实际条目的示例:

  {"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],
...

我无法控制列表。我曾经将所有内容转换为json,然后将json存储在file / s中。但它很混乱,因此我决定将所有内容存储在MySQL数据库中。

我正在使用此脚本

        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)

这个脚本用于传输数据,一次一个地从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

但是我收到了这个错误:

_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')

我已经正式放弃尝试调试这个!

此外,我将list转换为字符串,然后将该字符串存储在MySQL数据库表(TEXT)中。但是idk如果这是最好的方式,就像一个学生,说Aron决定他在文学中有问题然后我将不得不拉出列表(存储为字符串),然后将其转换为列表然后转换它再次返回字符串,然后首先删除那里的字符串,并用新的字符串更新它。有没有更好的方法呢?

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]] |
+---------------+---------+---------+------------------------------------------------------------------------------------------+

请,任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:0)

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