Skip to content

Instantly share code, notes, and snippets.

@leeclemens
Created February 5, 2021 03:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leeclemens/812027ba306363de61266135e31e30c9 to your computer and use it in GitHub Desktop.
Save leeclemens/812027ba306363de61266135e31e30c9 to your computer and use it in GitHub Desktop.
mariadb-connector-python char type bytes if prepared
import mariadb
HOST = ''
PORT = ''
USER = ''
PASSWORD = ''
DATABASE = ''
"""
CREATE TABLE `t` (
`id` SMALLINT UNSIGNED NOT NULL AUTO_INCREMENT,
`c` CHAR(8) NOT NULL,
`tt` TINYTEXT NOT NULL,
CONSTRAINT PRIMARY KEY (`id`))
;
INSERT INTO `t` (`c`, `tt`) VALUES ('12345678', '12345678');
INSERT INTO `t` (`c`, `tt`) VALUES ('deadbeef', 'cafebabe');
"""
BASE_QRY = 'SELECT `c`, `tt` FROM `t`'
WHERE_QRY = "SELECT `c`, `tt` FROM `t` WHERE `c` = '12345678'"
PREPARED_QRY = 'SELECT `c`, `tt` FROM `t` WHERE `c` = ?'
PREPARED_DATA = ('deadbeef',)
def main():
conn = mariadb.connect(host=HOST,
port=PORT,
user=USER,
password=PASSWORD,
database=DATABASE)
test_base(conn)
test_where(conn)
test_prepared(conn)
conn.close()
def test_base(conn):
print('Testing BASE_QRY')
cur = conn.cursor()
cur.execute(BASE_QRY)
for row in cur.fetchall():
print([type(c) for c in row])
cur.close()
def test_where(conn):
print('Testing WHERE_QRY')
cur = conn.cursor()
cur.execute(WHERE_QRY)
for row in cur.fetchall():
print([type(c) for c in row])
cur.close()
def test_prepared(conn):
print('Testing PREPARED_QRY')
cur = conn.cursor(prepared=True)
cur.execute(PREPARED_QRY, PREPARED_DATA)
for row in cur.fetchall():
print([type(c) for c in row])
cur.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment