3

I am new to Python 2.6. I have been trying to fetch date datetime value which is in yyyy-mm-dd hh:m:ss format back in my Python program. On checking the column type in Python I get the error: 'buffer' object has no attribute 'decode'. I want to use the strptime() function to split the date data and use it but I can't find how to convert a buffer to string. The following is a sample of my code (also available here):

conn = sqlite3.connect("mrp.db.db", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
qryT = """
    SELECT dateDefinitionTest FROM t
    WHERE IDproject = 4 AND IDstatus = 5
    ORDER BY priority, setDate DESC
"""
rec = (4,4)
cursor.execute(qryT,rec)
resultsetTasks = cursor.fetchall()
cursor.close()  # closing the resultset
for item in resultsetTasks:
    taskDetails = {}
    _f = item[10].decode("utf-8")

The exception I get is:

'buffer' object has no attribute 'decode'
3
  • Please show your code and the exact error. Commented Nov 11, 2012 at 11:58
  • pastie.org/5360165 dateDefinitionTest is a sqlite3 field of datetime Commented Nov 11, 2012 at 12:03
  • Error i get: ** 'buffer' object has no attribute 'decode' ** Commented Nov 11, 2012 at 12:04

2 Answers 2

2

I am not exactly sure what your problem may be. The following is a working example of what you are trying to achieve, which hopefully will help you:

#!/usr/bin/env python

import datetime
import sqlite3

conn = sqlite3.connect(":memory:", detect_types=sqlite3.PARSE_DECLTYPES)
cursor = conn.cursor()
cursor.execute("CREATE TABLE t (dateDefinitionTest DATETIME)")
cursor.execute("INSERT INTO t VALUES (?)", (datetime.datetime.now(),))
query = "SELECT dateDefinitionTest FROM t"
cursor.execute(query)
for row in cursor.fetchall():
    dt = datetime.datetime.strptime(row[0], "%Y-%m-%d %H:%M:%S.%f")
    print(repr(dt))
    print(dt.strftime("%Y-%m-%d %H:%M:%S.%f"))
cursor.close()

which outputs:

datetime.datetime(2012, 11, 11, 16, 40, 26, 788966)
2012-11-11 16:40:26.788966
Sign up to request clarification or add additional context in comments.

9 Comments

I did same but getting the error: strptime() argument 1 must be string, not buffer . I m using Python 2.6
What I did, i assigned row[0] in another variable: f = row[0][:] that converted buffer into str and then strptime accepted the value.
My example was tested with Python 2.7. It may be that Python 2.6 returns a buffer instead of a string.
yes seems version issue. Can you please tell how to format datetime.datetime(2012, 11, 11, 13, 9, 29, 141720)? strftime() does not work as it's being returned as tuple
I guesss I found the reason. I was importing as from datetime import datetime,date, time and i think last import was causing the overriding of some function within datetime. By removing extra module import it's now seems to work
|
2

Your problem's root cause is considering the 'buffer' object gotten from Sqlite database as a 'string' object, the string object has the encode() method, but 'buffer' object has no this method. What your need do is simple: just convert the 'buffer' object to string object, and it's not difficult, only add one line in your codes:

tempString=str(item[10])

_f = tempString.decode("utf-8")

I encountered the same problem today, and googled directed me to here, and found no suitable answer yet. So provide it here. The sqlite record data's type is buffer or string is decided by how we construct our db table, and the sqlite version, and the sqlite db plugin's version, so you test result is different from Pedro Romano's. But any way, just add this line: tempString=str(item[10]), it could force the system use it as a string.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.