Updating a Python SQLITE3 Database

Опубликовано: 16 Апрель 2021
на канале: ZeroLife
981
5

import sqlite3, json, base64

DFLT_data = {'username':'Reza','something':'nothing'}

def convertToBinaryThenBase64(filename):
Convert digital data to binary format then base64 formate
with open(filename, 'rb') as fout:
blobData = fout.read()
base64_data = base64.b64encode(blobData)
return base64_data

def db_update(db_name='c:/mydatabase.db', table='mytable',
data=DFLT_data, cat=['*'], search=['*']):
table = 'db' + str(table)

data_val = []
for item in data:
if isinstance(data[item], str) and (data[item][1:3] == f':{os.sep}'):
data_val.append(convertToBinaryThenBase64(data[item]))
elif isinstance(data[item], (list,dict,set)):
data_val.append(json.dumps(data[item]))
else:
data_val.append(data[item])

temp = []
for item in data:
temp.append(f'{item} = ?')
columns = ' ,'.join(temp)
#columns = ', '.join([f'{item} = ?' for item in data])

if cat == ['*'] and search ==['*']:
conditions = ''
#UPDATE dbmytable SET username = ?, something = ?
elif len(cat) == 1 and len(search) greaterThan 1:
data_val.extend(search)

temp = []
for _ in range(len(search)):
temp.append('?')
q_marks = ','.join(temp)
#q_marks = ','.join(['?' for _ in range(len(search))])
conditions = 'WHERE ' + f'{cat[0]} IN ({q_marks})'

#UPDATE dbmytable SET username = ?, something = ? WHERE username in (?,?,?..)
elif len(cat) greaterThan= 1 and len(cat) == len(search):
data_val.extend(search)
temp = []
for ct in cat:
temp.append(f'{ct} = ?')
condition2 = ' OR '.join(temp)
conditions = 'WHERE ' + condition2
#conditions = 'WHERE ' + ' OR '.join([f'{ct} = ?' for ct in cat])
#UPDATE dbmytable SET username = ?, something = ? WHERE username = ? OR something = ?
else:
print('something went wrong')

string_execute = f'UPDATE {table} SET {columns} {conditions}'

conn = sqlite3.connect(db_name)
cursor = conn.cursor()
with conn:
cursor.execute(string_execute, data_val)

return string_execute

PAT:   / rezatahirkheli  
PP: https://www.paypal.com/paypalme/rezat...
BTC: 3EUQBWZKX9Vcwdffd3cUGATQopxrAQQxJ9
LTC: MBXE6hJgxxFYSD8SjmR7sHCHGTqFXmNGoU
GFM: https://gofund.me/7d6500bd


Смотрите видео Updating a Python SQLITE3 Database онлайн без регистрации, длительностью часов минут секунд в хорошем качестве. Это видео добавил пользователь ZeroLife 16 Апрель 2021, не забудьте поделиться им ссылкой с друзьями и знакомыми, на нашем сайте его посмотрели 981 раз и оно понравилось 5 людям.