Store Japanese (multibyte character string) in sqlite3 of python

If you do "Make the string unicode and then pass it to the sqlite3 module" It can handle multi-byte character strings without any problem.

python


import sqlite3,sys,os

con = sqlite3.connect("./temp.sqlite")

#Create table
sql = u"""
  create table filepath(
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    path text);"""
con.execute(sql)

#Receive file list from standard input
#Store in DB
for rwln in iter(sys.stdin.readline,""):
  path = rwln.rstrip('\n') #Remove trailing newline
  sql = u"""insert into filepath(path) values (?); """
  cur = self.con.cursor()
  #File system encoding is sys.getfilesystemencoding()Because you can get it with
  #Use this to unicode()After conversion by function, pass it to sqlite module
  cur.execute(sql,(unicode(path, sys.getfilesystemencoding()),))

Besides this method, it can be stored after base64 conversion, If the encoding can be specified, it is better to use unicode.

Recommended Posts

Store Japanese (multibyte character string) in sqlite3 of python
Conversion of string <-> date (date, datetime) in Python
Basic grammar of Python3 system (character string)
Sqlite in python
[Introduction to Python] Thorough explanation of the character string type used in Python!
Calculation of match rate of character string breaks [python]
String manipulation in python
Japanese output in Python
Divides the character string by the specified number of characters. In Ruby and Python.
[Python] How to expand variables in a character string
I wrote python in Japanese
String object methods in Python
Use parameter store in Python
Equivalence of objects in Python
2.x, 3.x character code of python
String date manipulation in Python
I understand Python in Japanese!
Implementation of quicksort in Python
Character code learned in Python
Python f character (formatted string)
Get Japanese synonyms in Python
Find out the apparent width of a string in python
Sort the string array in order of length & Japanese syllabary
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
How to quickly count the frequency of appearance of characters from a character string in Python?
Specify the encoding of the unicode string in the Python 2.x print statement
Pixel manipulation of images in Python
Summary of character string format in Python3 Whether to live with the old model or the new model
[Introduction to Python] How to output a character string in a Print statement
Division of timedelta in Python 2.7 series
How to use SQLite in Python
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
6 ways to string objects in Python
Foreign Key in Python SQLite [Note]
Law of large numbers in python
Implementation of original sorting in Python
Reversible scrambling of integers in Python
Create a random string in Python
How to handle Japanese in Python
[Python] How to change character string (str) data to date (strptime of datetime)
I want to batch convert the result of "string" .split () in Python
I want to color a part of an Excel string in Python
Check the behavior of destructor in Python
Handling of character code of file in IronPython
(Bad) practice of using this in Python
General Theory of Relativity in Python: Introduction
Output tree structure of files in Python
(Java, JavaScript, Python) Comparison of string processing
Display a list of alphabets in Python 3
Python UTC ⇔ JST, character string (UTC) ⇒ JST conversion memo
Summary of various for statements in Python
In python + sqlite3 "OperationalError: no such column:"
# 5 [python3] Extract characters from a character string
python string comparison / use'list'and'in' instead of'==' and'or'
[Python] How to invert a character string
[Python beginner memo] Python character string, path operation
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python