1

I tried to venture off while following a python-flask tutorial and am having trouble figuring out an error. I tried browsing stack for 6 hours off and on and nothing out there related could help me out, possibly because I am a beginner...

I gathered data from a form (all the data is passing through fine, I tested by printing to the console) and want to insert it into a mysql table.

Here is the query code:

c.execute("INSERT INTO posts (post_count, seller, title, id, bus, condition, transType, post_description, trade, price) VALUES (%i, %s, %s, %s, %s, %s, %s, %s, %s, %i)", (thwart(postCount), thwart(seller), thwart(title), thwart(id), thwart(bus), thwart(condition), thwart(transType), thwart(description), thwart(tradeFor), thwart(price)))

The table description in MySql is:

+------------------+-------------+------+-----+---------+----------------+
| Field            | Type        | Null | Key | Default | Extra          |
+------------------+-------------+------+-----+---------+----------------+
| post_id          | int(11)     | NO   | PRI | NULL    | auto_increment |
| post_count       | int(11)     | YES  |     | NULL    |                |
| seller           | varchar(60) | YES  |     | NULL    |                |
| title            | text        | YES  |     | NULL    |                |
| id               | varchar(25) | YES  |     | NULL    |                |
| bus              | varchar(35) | YES  |     | NULL    |                |
| condition        | varchar(20) | YES  |     | NULL    |                |
| transType        | varchar(25) | YES  |     | NULL    |                |
| post_description | text        | YES  |     | NULL    |                |
| trade            | text        | YES  |     | NULL    |                |
| price            | int(11)     | YES  |     | NULL    |                |
+------------------+-------------+------+-----+---------+----------------+

The error I keep getting is: must be string or read-only buffer, not long

I don't see where the long is coming from...Not sure what else you may need to help me out so ask if there's more info I can add.

much appreciated!

jmunsch
  • 22,771
  • 11
  • 93
  • 114
user3344239
  • 109
  • 1
  • 10
  • 1
    Check all the variables `type` which are being inserted. I would guess one of them is suppose to be `text` or `varchar`, but is a `long`. try converting the `long` to a `str` basically one of the numbers being inserted isnt text but might look like `37582L` – jmunsch Sep 26 '16 at 06:28
  • See: http://stackoverflow.com/questions/2104884/how-does-python-manage-int-and-long – jmunsch Sep 26 '16 at 06:33
  • I just tried casting every single var but it didn't work. thanks for the idea though...@jmunsch – user3344239 Sep 26 '16 at 06:40
  • 1
    If you alter every one to `str` then those that have to be long will fail! Only "cast" the text and varchar variables. – cdarke Sep 26 '16 at 06:48
  • Well, I casted everything that is str or varchar to str and casted int's as int's. i removed the casting of the ints and i am getting the same error :( @cdarke – user3344239 Sep 26 '16 at 16:52
  • does this clue help? I tried printing the TYPE of each variable and they are 'unicode'.....? @cdarke – user3344239 Sep 26 '16 at 16:56
  • @jmunsch Does it help that I ran a 'print(type(variable)) on all of the variables and the type for all is unicode? – user3344239 Sep 26 '16 at 16:58
  • If you create string objects (using `str()`) from the unicode then that should make no difference. The error message you show is complaining about the numerics, not the text strings. If the numerics are unicode, then when you create `int` objects from the unicode (using `int()`) we should be OK. – cdarke Sep 26 '16 at 17:15
  • I found it! @jmunsch, you were first, and your suggestion was correct so if you post as an answer, I will accept it. I found post_count was for some reason type long and had to be casted to string. ALSO, it turns out 'condition' is a reserved word in MySQL so I had to change that column name as well. – user3344239 Sep 26 '16 at 18:07
  • @user3344239 great glad you were able to find it. – jmunsch Sep 26 '16 at 21:50

1 Answers1

0

Check all the variables type which are being inserted.

I would guess one of them is suppose to be text or varchar, but is a long.

Try converting the long to a str basically one of the numbers being inserted isnt text but might look like 37582L

print(type(post_count))
print(type(seller))
print(type(post_description))

if type(some_long) is long:
    do_insert(str(some_long))
jmunsch
  • 22,771
  • 11
  • 93
  • 114