python에서 sqlite 파일을 접속해서 조작하는 방법


$ sudo apt-get install python-sqlite

Reading package lists... Done

Building dependency tree

Reading state information... Done

The following extra packages will be installed:

  libsqlite0

Suggested packages:

  python-sqlite-dbg

The following NEW packages will be installed:

  libsqlite0 python-sqlite

0 upgraded, 2 newly installed, 0 to remove and 7 not upgraded.

Need to get 141 kB of archives.

After this operation, 519 kB of additional disk space will be used.

Do you want to continue? [Y/n]

Get:1 http://mirrordirector.raspbian.org/raspbian/ jessie/main libsqlite0 armhf 2.8.17-12 [119 kB]

Get:2 http://mirrordirector.raspbian.org/raspbian/ jessie/main python-sqlite armhf 1.0.1-11 [21.3 kB]

Fetched 141 kB in 1s (95.8 kB/s)

Selecting previously unselected package libsqlite0.

(Reading database ... 137326 files and directories currently installed.)

Preparing to unpack .../libsqlite0_2.8.17-12_armhf.deb ...

Unpacking libsqlite0 (2.8.17-12) ...

Selecting previously unselected package python-sqlite.

Preparing to unpack .../python-sqlite_1.0.1-11_armhf.deb ...

Unpacking python-sqlite (1.0.1-11) ...

Setting up libsqlite0 (2.8.17-12) ...

Setting up python-sqlite (1.0.1-11) ...

Processing triggers for libc-bin (2.19-18+deb8u7) ...


pi@raspberrypi:~ $ cd src

pi@raspberrypi:~/src $ ll

total 2060

drwxr-xr-x  2 pi pi    4096 Mar 30 14:40 .

drwxr-xr-x 30 pi pi    4096 Mar 30 14:34 ..

-rw-r--r--  1 pi pi 2099200 Mar 29 12:29 sqlite.udb


pi@raspberrypi:~/src $ python

Python 2.7.9 (default, Sep 17 2016, 20:26:04)

[GCC 4.9.2] on linux2

Type "help", "copyright", "credits" or "license" for more information.

>>> import sqlite3

>>> conn = sqlite3.connect('sqlite.udb')

>>> c = conn.cursor()

>>> c.execute("select * from tbl_recv")

<sqlite3.Cursor object at 0x769eaa20> 


데이터가 유니코드로 나오나..

# we can also implement a custom text_factory ...

# here we implement one that will ignore Unicode characters that cannot be

# decoded from UTF-8

con.text_factory = lambda x: unicode(x, "utf-8", "ignore") 


[링크 : https://docs.python.org/2/library/sqlite3.html]

'Programming > python(파이썬)' 카테고리의 다른 글

파이썬 리스트(list)와 튜플(tuple)  (0) 2017.04.02
파이썬 type 확인하기  (0) 2017.04.02
python smtplib의 신비..?  (0) 2016.12.30
python이 인기라는데..  (0) 2014.03.19
python2 vs python3  (0) 2013.01.02
Posted by 구차니