Python Forum
Help with searching tables in Sqlite
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help with searching tables in Sqlite
#1
I want to make a function that will search all tables of a sqlite database for a string, if it does not find that striing, then it will write it into one of the tables , i was going this way i don't know if it is a good direction:

import sqlite3
import random

conn = sqlite3.connect('listatest.db')
c = conn.cursor()

tablelist = ('unu', 'doi', 'trei','patru')
echipa1= "test3"
echipa2 = "test2"
scor1="testsc1"
scor2 ="testsc2"
cota =5
miza = 3
stare = 2

def creat_tables():

    for tablename in tablelist:
        create = 'CREATE TABLE IF NOT EXISTS ' + tablename + ' (numar_par INTEGER PRIMARY KEY, echipa1 TEXT, scor1 TEXT, scor2 TEXT, echipa2 TEXT,cota INT, miza INT, stare INT)'

        c.execute(create)

def incarca():
    for tablename in tablelist:
        c.execute('SELECT * FROM ' + tablename + ' WHERE echipa1 = ?', (echipa1,))
        xx = c.fetchone()
        if xx is None:
            z = 1
        if z == 1:
            c.execute( "INSERT INTO " + tablename + "( echipa1,scor1, scor2, echipa2, cota, miza, stare  ) VALUES (?,?,?,?,?,?,?)",
                (echipa1, scor1, scor2, echipa2, cota,
                 miza, stare))
            conn.commit()
            c.execute('SELECT * FROM ' + tablename + ' WHERE echipa1 = ?', (echipa1,))
            xx = c.fetchone()
            if xx is None:
                z = 1
            else:
                z=0
      
      
Right now my code will write the values in all tables, but i want to write it only in one table
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020