简体   繁体   English

如何使用python在mysql中存储一个巨大的字符串(长度为50000)

[英]how to store a huge string (length 50000) in mysql using python

how to store a huge string (length 50000) in mysql using python.如何使用python在mysql中存储一个巨大的字符串(长度为50000)。 I have a big string of length nearly 50000 .I have to store it into mysql.我有一个长度接近 50000 的大字符串。我必须将它存储到 mysql 中。 Some suggested to store the string as a blob or text type.有人建议将字符串存储为 blob 或文本类型。

Can anyone help me how to convert string into blob type任何人都可以帮助我如何将字符串转换为 blob 类型

def main():
    stringKey=''
    stringValues=''
    keys=ccv.keys()        //ccv is a dictionary data structure  
    vectors=ccv.values()  //ccv is a dictionary data structure 
    for key in keys:
            stringKey='#'.join(key for key in keys)
    for value in vectors:
            stringValues='$'.join(str(value) for value in vectors)
    insert(stringKey,stringValues)
    print 'insert successful'

def insert(k,v):
    db = mysql.connector.connect(user='root', password='abhi',
                              host='localhost',
                              database='cbir')

    sql= 'INSERT INTO ccv(key,vector) VALUES(%s,%s)'
    args = (k,v)
    cursor=db.cursor()
    cursor.execute(sql,args)
    db.commit()
    db.close()

error:错误:

ProgrammingError: 1064 (42000): 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 'key,vector) VALUES('27:1:8#27:1:9#25:2:11#6:9:8#6:9:9#6:9:6#6:9:7#6:9:4#6:9:5#27' at line 1

You made a small error in:你犯了一个小错误:

'INSERT INTO ccv(key,vector) VALUES(%s,%s)'

It should be:它应该是:

"INSERT INTO ccv (`key`, `vector`) VALUES(%s, %s)"

Notice the ` denoting the column names.注意`表示列名。

As Larry reminded me the values don't have to be quoted for parameterized queries.正如 Larry 提醒我的那样,不必为参数化查询引用这些值。

If the fields are already set for longtext this should work without needing to convert the data to blobs.如果字段已经设置为长文本,这应该可以工作,而无需将数据转换为 blob。

The problem was only related to the syntax and not the data or column types.问题仅与语法有关,与数据或列类型无关。

您不需要转换字符串,您需要将数据库中的字段类型从 varchar 更改为 longtext。

You are not associating the arguments with the SQL statement.您没有将参数与 SQL 语句相关联。

You want你要

 cursor.execute(sql, args)

instead of代替

 cursor.execute(sql)
sql = "INSERT INTO ccv(`key`,`vector`) VALUES(%s,%s)"

这工作正常

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

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