Git Product home page Git Product logo

Comments (11)

rogerbinns avatar rogerbinns commented on July 18, 2024

From rogerbinns on July 17, 2008 13:24:31

Since you are supplying a Python string, it has to be converted to Unicode since that
is the only type of string that SQLite accepts. (This is before any column affinity
rules are applied by SQLite).

To supply binary data use the buffer class - in your example:

buffer("\xcf\xde")

I will update the documentation section Types to be a little clearer as well as add
something to the example.

Status: Accepted
Labels: Component-Docs

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From [email protected] on July 18, 2008 03:32:57

I'm afraid this doesn't work for me:

c.execute("insert into blobs values(?)", buffer("\xFE"))
Traceback (most recent call last):
File "", line 1, in
UnicodeDecodeError: 'ascii' codec can't decode byte 0xfe in position 0: ordinal not
in range(128)

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From rogerbinns on July 18, 2008 03:38:05

That is because you are not providing a tuple of items like in your first post :-)
It should be:

c.execute("insert into blobs values(?)", (buffer("\xFE"), ) )

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From [email protected] on July 18, 2008 10:39:19

True, now it works indeed. Thanks a lot!

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From [email protected] on July 18, 2008 10:49:21

This might be a stupid question, but how can I get back a (byte) string from the
buffer that I get when I query the table?

I couldn't find any documentation of the buffer objects on python.org/doc...

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From rogerbinns on July 18, 2008 13:15:44

In Python 2 you can turn a buffer into a string by using str or [:]. For example:

x=buffer("abc")
print type( str(x) )
print type( x[:] )

If you look in the index in the Python documentation you'll find some measly doc.

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From [email protected] on July 19, 2008 01:34:22

Your example works nicely, but for some reason it does not work if I apply it to the
buffer object returned by the cursor:

c.execute("insert into blobs values(?)", (buffer("\xCF"),))
<apsw.Cursor object at 0xb7db6d10>
res = c.execute("select * from blobs")
a = res.next()
a
(<read-write buffer ptr 0xb7db8d10, size 1 at 0xb7db8cf0>,)
str(a)
'(<read-write buffer ptr 0xb7db8d10, size 1 at 0xb7db8cf0>,)'

I'm using Python 2.5.2

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From rogerbinns on July 19, 2008 01:37:43

The cursor iterator returns an entire row as a tuple. You called str on that so that
is exactly what you have printed out - a tuple containing one buffer item.

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From [email protected] on July 19, 2008 01:55:19

Brrr... You're of course right again. I probably shouldn't try to learn Python and
apsw at the same time. Thanks a lot for your help and patience!

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From rogerbinns on July 19, 2008 02:47:17

There is no problem learning Python and APSW at the same time. Python itself is very
simple although it takes a while to learn "best practise". The paper version of the
Python Cookbook is a good tour of various best practises.

Your "mistake" is assuming that whenever you encounter issues they are bugs. The
chances of you finding bugs in Python, APSW, SQLite etc after just starting using
them is extremely low compared to the underlying issue being in your code or
understand of how things work :-)

from apsw.

rogerbinns avatar rogerbinns commented on July 18, 2024

From rogerbinns on July 26, 2008 02:47:44

Doc updated in r190

Status: Verified

from apsw.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.