Relationship between SQLite and Python 2.X -


i little puzzled relationship between sqlite3 , python 2.x

here output:

$ python python 2.7.2+ (default, jul 20 2012, 22:12:53)  [gcc 4.6.1] on linux2 type "help", "copyright", "credits" or "license" more information. >>> import sqlite3 >>> sqlite3.version '2.6.0' >>> sqlite3.sqlite_version '3.7.7' 

the '3.7.7' supposed version of sqlite on ubuntu os.

however, when tried

sqlite3 test.db  bash: sqlite3: command not found 

which means sqlite3 not installed on machine.

then how can use pragma statements "pragma encoding = "utf-8";" when creating database in python?

first:

the '3.7.7' supposed version of sqlite on ubuntu os.

no. it's version of sqlite library python built against.

if python built against static library, there's no reason expect related in way else on system. if built against shared library, doesn't have same shared library that's used distro's default sqlite package (although typically be).

and, if python was built against same shared lib that's distro's default shared lib: ubuntu, many distros, allows install libfoo.so shared libraries without installing foo command-line tool.

in particular, if @ ubuntu packages (at least recent versions), there sqlite shared libs in libsqlite3 package, , both python , sqlite3 depend on libsqlite3 package.

so, if want sqlite3 command-line tool, have install it; don't automatically installing python.


meanwhile:

is there way use pragma encoding = "utf-8" inside python code?

sure. pragma sql statement affects rest of session. doesn't matter whether execute in command-line tool, or in python execute command, or loading sql script, etc. execute it.

however, point out in comments, according the docs, particular pragma won't have effect unless can run before connecting database. because db-api 2.0 (the api python sqlite3 module, , other python database modules, built to) doesn't have way of executing statements in unconnected state, there's no easy way around this.

you can, of course, various alternate (generally not tested, , slower) sqlite3 implementations python, or fork , modify the source pysqlite (the module periodically included stdlib sqlite3).

if want this, place pysqlite_connection_init function in connection.c. don't want add general-purpose pre-connect pragma support, rather way control encoding via parameter connect. see open docs sqlite3 see can usefully here. (and want file bug , submit patch. won't ever make python 2.x, gets accepted in pysqlite end in python 3.x.)

but easier way want trying do: create database outside of python. install sqlite3 package in favorite way (apt-get, 1 of gui wrappers, whatever) , use it.


one last thing: can tell, depending on version, python's module either use utf-8 explicitly, or use default encoding, utf-8. so, don't need here.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -