【问题标题】:Unable to add list of list in mysql database无法在mysql数据库中添加列表列表
【发布时间】:2015-12-05 14:22:22
【问题描述】:

我有一个字典,其格式如下:

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

它的键是学生的name,映射到该键的值是一个list,它在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')

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

另外,我将列表转换为字符串,然后将该字符串存储在 MySQL 数据库表(TEXT)中。但是如果这是最好的方法,就好像一个学生说 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]] |
+---------------+---------+---------+------------------------------------------------------------------------------------------+

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

【问题讨论】:

    标签: python mysql json


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 2017-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-10-13
      • 2020-02-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多