def delMember(member): name = member.getVal('name') party.remove(member) with sqlite3.connect('party.db') as db: cursor = sqlite3.cursor(db) cursor.execute(f"DELETE FROM MEMBERS WHERE name='{name}'") db.commit()
def find(value): db = Track.get_connection() c = db.cursor() c.execute(''' select key, value, file_id from tags where file_id in ( select file_id from tags where value like ?) ''', ('%{}%'.format(value),)) data = {} for key, value, file_id in c: if file_id not in data: data[file_id] = {key: value} elif key not in data[file_id]: data[file_id][key] = [value] else: data[file_id][key].append(value) ans = [] for file_id, meta in data.items(): uri, = c.execute(''' select uri from files where rowid=? ''', (file_id,)).fetchone() t = Track(uri, meta) t.id = file_id ans.append(t) return ans
def save(self): db = Track.get_connection() c = db.cursor() try: c.execute(''' insert or abort into files (uri) values (:uri) ''', {'uri': self.uri} ) except db.IntegrityError: pass db.commit() self.id, = c.execute(''' select rowid from files where uri=:uri ''', {'uri': self.uri}).fetchone() for k, values in self.metadata.items(): for v in values: try: c.execute(''' insert into tags (key, value, file_id) values (:key, :value,:file_id) ''', {'key':k, 'value': v, 'file_id': self.id}) except Exception as e: print ('Exception:', e) print('Error:', k, v, self.id) db.commit()
def count(): db = Track.get_connection() c = db.cursor() total, = c.execute(''' select count(*) from files ''').fetchone(); return total
def addMember(typee): party.append(globals()[typee]()) name = party[-1].getVal('name') hp = party[-1].getVal('hp') power = party[-1].getVal('power') SApower = party[-1].getVal('SApower') speed = party[-1].getVal('speed') with sqlite3.connect('party.db') as db: cursor = sqlite3.cursor(db) cursor.execute(f"INSERT INTO MEMBERS VALUES('{name}', '{hp}', '{power}', '{SApower}', '{speed}', '{typee}')") db.commit()
def all(): db = Track.get_connection() c = db.cursor() c.execute('select uri, rowid from files') tracks = [] for uri, file_id in c: meta = {} meta_c = db.cursor() meta_c.execute(''' select key, value from tags where file_id=:file_id ''', {'file_id':file_id}) for key, value in meta_c: if key not in meta: meta[key] = [value] else: meta[key].append(value) tracks.append(Track(uri, meta)) return tracks
def runQuery(query): try: db = mysql.connector.connect( host='localhost', database='db_theatre', user='******', password='******') if db.is_connected(): cursor = db.cursor(buffered = True) cursor.execute(query) db.commit() return cursor.fetchall() except Error as e: #Some error occured return e.args[1] finally: db.close() #Couldn't connect to MySQL return None
def getContact(self,Contact): contact = Contact.getContact(uid) db = SQLiteDatabase.getReadableDatabase() cursor = SQLiteDatabase.cursor() # cursor is used to iterate through db table elements cursor = db.query(Util.TABLE_NAME,list((Util.KEY_ID,Util.KEY_NAME, Util.KEY_PHONE_NUMBER)),Util.KEY_ID +"=?", list(String.valueOf(id)),None,None,None) if cursor != None: cursor.moveToFirst() contact = Contact() contact.setId(Integer.parseInt(cursor.getString(0))) contact.setName(cursor.getString(1)) contact.setPhoneNumber(cursor.getString(2)) cursor.close() return contact
import sqlite3 as d d = d.connect('database/database.db') c = d.cursor() c.execute('DROP TABLE IF EXISTS post') c.execute("CREATE TABLE post(id INT PRIMARY KEY AUTOINCREMENT, data VARCHAR(80), date_post DATE)") c.close() print "success make db"
import sqlite3 as d d = d.connect('database/database.db') c = d.cursor() c.execute('DROP TABLE IF EXISTS post') c.execute( "CREATE TABLE post(id INT PRIMARY KEY AUTOINCREMENT, data VARCHAR(80), date_post DATE)" ) c.close() print "success make db"
#Authur AmirArab # What needed for whole program import datetime ### What I have learnt from Class: os / export import os from tabulate import tabulate import sqlite3 as db db = db.connect('Lib.db') lib = db.cursor() Today = datetime.datetime.now() twoWeeks = Today + datetime.timedelta(weeks=2) def show_books(): lib.execute(''' select * from Books ''') all_rows = lib.fetchall() lists = list(all_rows) names = [description[0] for description in lib.description] books = tabulate(lists, headers=names) print(books) def show_members(): lib.execute(''' select * from Members ''') all_rows = lib.fetchall()