Read data from a SQLite 3 database in Python

0

I'm starting in Python and database and I've had three doubts!

  • For example, inside my database I have a "name" column. Just to illustrate, let's say I want to get the contents of the "name" column corresponding to id = 1 and play on a Python variable called "clientName"?

  • How do I list the total number of existing records?

  • If I want to make a button in Python that navigates between records How should it be done? Is there something like cursor.next and cursor.previous, for example?

  • Thank you, guys!

        
    asked by anonymous 15.12.2017 / 03:40

    1 answer

    0

    I've done this, I think it's the simplest, uses more Python code than SQL features.

    import sqlite3
    
    conn = sqlite3.connect("teste.db")
    cursor = conn.cursor()
    
    cursor.execute("create table if not exists testes ( nome varchar(50));")
    
    for i in range(1, 1001):
        cursor.execute("insert into testes values (' testando like a boss %s')" %i)
    
    cursor.execute("select * from testes")
    
    count = 0
    
    for row in cursor.fetchall():
        print(row)
        count += 1
        if (count % 10) == 0:
            escolha = input('c - continuar, p - parar? \n')
            if escolha.lower() == 'c':
                pass
            elif escolha.lower() == 'p':
                break
    conn.commit()
    
    conn.close()
    

    I hope it helps.

        
    15.12.2017 / 04:20