change all cursor's encoding to 'utf8'

This commit is contained in:
Tyrone-Zhao 2018-09-22 14:24:32 +08:00
parent c124d53aff
commit b52b8ef7bd
2 changed files with 11 additions and 11 deletions

View file

@ -2,7 +2,7 @@
"database": { "database": {
"host": "127.0.0.1", "host": "127.0.0.1",
"user": "root", "user": "root",
"passwd": "", "passwd": "12345678",
"db": "dejavu" "db": "dejavu"
} }
} }

View file

@ -154,7 +154,7 @@ class SQLDatabase(Database):
This also removes all songs that have been added but have no This also removes all songs that have been added but have no
fingerprints associated with them. fingerprints associated with them.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.CREATE_SONGS_TABLE) cur.execute(self.CREATE_SONGS_TABLE)
cur.execute(self.CREATE_FINGERPRINTS_TABLE) cur.execute(self.CREATE_FINGERPRINTS_TABLE)
cur.execute(self.DELETE_UNFINGERPRINTED) cur.execute(self.DELETE_UNFINGERPRINTED)
@ -177,14 +177,14 @@ class SQLDatabase(Database):
""" """
Removes all songs that have no fingerprints associated with them. Removes all songs that have no fingerprints associated with them.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.DELETE_UNFINGERPRINTED) cur.execute(self.DELETE_UNFINGERPRINTED)
def get_num_songs(self): def get_num_songs(self):
""" """
Returns number of songs the database has fingerprinted. Returns number of songs the database has fingerprinted.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.SELECT_UNIQUE_SONG_IDS) cur.execute(self.SELECT_UNIQUE_SONG_IDS)
for count, in cur: for count, in cur:
@ -195,7 +195,7 @@ class SQLDatabase(Database):
""" """
Returns number of fingerprints the database has fingerprinted. Returns number of fingerprints the database has fingerprinted.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.SELECT_NUM_FINGERPRINTS) cur.execute(self.SELECT_NUM_FINGERPRINTS)
for count, in cur: for count, in cur:
@ -207,7 +207,7 @@ class SQLDatabase(Database):
Set the fingerprinted flag to TRUE (1) once a song has been completely Set the fingerprinted flag to TRUE (1) once a song has been completely
fingerprinted in the database. fingerprinted in the database.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.UPDATE_SONG_FINGERPRINTED, (sid,)) cur.execute(self.UPDATE_SONG_FINGERPRINTED, (sid,))
def get_songs(self): def get_songs(self):
@ -231,14 +231,14 @@ class SQLDatabase(Database):
""" """
Insert a (sha1, song_id, offset) row into database. Insert a (sha1, song_id, offset) row into database.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.INSERT_FINGERPRINT, (hash, sid, offset)) cur.execute(self.INSERT_FINGERPRINT, (hash, sid, offset))
def insert_song(self, songname, file_hash): def insert_song(self, songname, file_hash):
""" """
Inserts song in the database and returns the ID of the inserted record. Inserts song in the database and returns the ID of the inserted record.
""" """
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(self.INSERT_SONG, (songname, file_hash)) cur.execute(self.INSERT_SONG, (songname, file_hash))
return cur.lastrowid return cur.lastrowid
@ -252,7 +252,7 @@ class SQLDatabase(Database):
# select all if no key # select all if no key
query = self.SELECT_ALL if hash is None else self.SELECT query = self.SELECT_ALL if hash is None else self.SELECT
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
cur.execute(query) cur.execute(query)
for sid, offset in cur: for sid, offset in cur:
yield (sid, offset) yield (sid, offset)
@ -272,7 +272,7 @@ class SQLDatabase(Database):
for hash, offset in hashes: for hash, offset in hashes:
values.append((hash, sid, offset)) values.append((hash, sid, offset))
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
for split_values in grouper(values, 1000): for split_values in grouper(values, 1000):
cur.executemany(self.INSERT_FINGERPRINT, split_values) cur.executemany(self.INSERT_FINGERPRINT, split_values)
@ -289,7 +289,7 @@ class SQLDatabase(Database):
# Get an iteratable of all the hashes we need # Get an iteratable of all the hashes we need
values = mapper.keys() values = mapper.keys()
with self.cursor() as cur: with self.cursor(charset="utf8") as cur:
for split_values in grouper(values, 1000): for split_values in grouper(values, 1000):
# Create our IN part of the query # Create our IN part of the query
query = self.SELECT_MULTIPLE query = self.SELECT_MULTIPLE