T
T
Timur Chernyaev2020-01-19 11:27:10
Python
Timur Chernyaev, 2020-01-19 11:27:10

How to create a database in python?

Tell me how to create a code that will display a specific item from a specific database in a table created using Tkinter (Treeview). The database was created in Access. Already used scripts are attached. In some, I just didn’t figure out where to shove what name)
Just in case, the name of the database is List, tables are Books, Users, Books subitems are Authors, Title, ISBN, Publisher, Year of issue, Quantity, Remaining; sub-items Users - Last name, First name, Patronymic, Year of birth, Class, Parallel (The application is created for the school). Thanks in advance

spoiler
import sqlite3
import tkinter as tk
import tkinter.ttk as ttk
  
  
class Table(tk.Frame):
    def __init__(self, parent=None, headings=tuple(), rows=tuple()):
        super().__init__(parent)
  
        table = ttk.Treeview(self, show="headings", selectmode="browse")
        table["columns"] = headings
        table["displaycolumns"] = headings
  
        for head in headings:
            table.heading(head, text=head, anchor=tk.CENTER)
            table.column(head, anchor=tk.CENTER)
  
        for row in rows:
            table.insert('', tk.END, values=tuple(row))
  
        scrolltable = tk.Scrollbar(self, command=table.yview)
        table.configure(yscrollcommand=scrolltable.set)
        scrolltable.pack(side=tk.RIGHT, fill=tk.Y)
        table.pack(expand=tk.YES, fill=tk.BOTH)
  
data = ('Users')
with sqlite3.connect('test.db') as connection:
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM Users")
    data = (row for row in cursor.fetchall())

root = tk.Tk()
table = Table(root, headings=('Фамилия', 'Имя', 'Отчество'), rows=data)
table.pack(expand=tk.YES, fill=tk.BOTH)
root.mainloop()


spoiler
import adodbapi
 
database = "db1.mdb"
constr = 'Provider=Microsoft.Jet.OLEDB.4.0; Data Source=%s'
tablename = "address"
 
conn = adodbapi.connect(constr)
 
cur = conn.cursor()
 
sql = "select * from %s" % tablename
cur.execute(sql)

result = cur.fetchall()
for item in result:
    print item
 
cur.close()
conn.close()

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
kiriharu, 2020-01-20
@kiriharu

As far as I remember, you can connect to the database on Access using pyodbc.

import pyodbc

conn = pyodbc.connect(r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\Users\kiriharu\Desktop\testdb.accdb;')
cursor = conn.cursor()
cursor.execute('select * from sometable')
   
for row in cursor.fetchall():
    print (row)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question