python-2.7 - 使用按钮在使用 Python 的访问数据库中创建一个新表

标签 python-2.7 tkinter ms-access-2007

我正在尝试在访问数据库中创建一个新表。
应该有两个按钮。

按钮 1(浏览):选择将在其中创建新表的 (.mdb) 文件

按钮 2(运行):创建新表

到目前为止我写的代码:

import pyodbc
from Tkinter import *
import tkFileDialog

def browse():
    A = tkFileDialog.askopenfilename()
    return str (A)

def run():
    conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=' + browse())
    cur = conn.cursor()

    if cur.tables(table = 'new').fetchone():
    cur.execute('DROP TABLE new')

    cur.execute('CREATE TABLE new( ID integer, Name string)')
    conn.commit()
    print (' New created')


r = Tk()
r.title ('test')
r.geometry ('200x300')

b1 = Button(r,text = 'Browse', command = browse).place (x = 10, y =10)
b2 = Button (r, text = 'run', command = run).place (x = 10, y =50)
r.mainloop()

问题是当我点击运行按钮时,它再次要求选择文件,运行按钮是否应该在以前选择的(使用浏览按钮)访问数据库中创建一个新表。如果有人能告诉我一种方法。我正在使用 Python 2.7 和 MS Access 2007。

最佳答案

我没有 pyodbc但其余代码正在运行。

我把它放在类里面以使其更干净。我改了一些名字 - run()create()因为我用的是名字 run()用于 mainloop() 的函数.

如果您使用 Create按钮打开 FileDialog仅当您之前没有选择文件时。

创建数据库程序后忘记文件名准备直接在Create中选择另一个文件名按钮

import pyodbc
from Tkinter import *
import tkFileDialog

class Application():

    def __init__(self, root):
        #print 'debug: __init__()'

        self.root = root

        self.root.title('Database Creator')
        self.root.geometry('300x300')

        self.b1 = Button(self.root, text='Browse', command=self.browse)
        self.b1.place(x=10, y=10)

        self.b2 = Button(self.root, text='Create', command=self.create)
        self.b2.place(x=10, y=50)

        self.filepath = None

    #----------------------

    def run(self):
        #print 'debug: run()'

        self.root.mainloop()

    #----------------------

    def browse(self):
        #print 'debug: browse()'

        self.filepath = tkFileDialog.askopenfilename()

        if self.filepath:
            print 'File selected:', self.filepath
        else:
            print 'File not selected'

    #----------------------

    def create(self):
        #print 'debug: create()'

        if not self.filepath:
            self.browse()

        if self.filepath:
            conn = pyodbc.connect('DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=' + self.filepath)
            cur = conn.cursor()

            if cur.tables(table = 'new').fetchone():
                cur.execute('DROP TABLE new')

            cur.execute('CREATE TABLE new( ID integer, Name string)')

            conn.commit()

            print ' New created'

            # now I will be ready to select another file
            self.filepath = None

#----------------------------------------------------------------------

Application(Tk()).run()

关于python-2.7 - 使用按钮在使用 Python 的访问数据库中创建一个新表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19845956/

相关文章:

vba - 如何遍历表单中的所有控件,包括子表单中的控件-Access 2007

matlab - 在 Python 中使用 matplotlib 绘制多个数据点

python - "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ' ' at line 1"

python - 用于可编辑依赖项更改的 pip egg 名称

python - 使用 ols(python statsmodel)是否适用于纵向数据和多个因变量?

ms-access - 在Access 2007表单中为按钮分配键盘快捷键

python - tkinter (python) 中的 IPAD-X/Y 和 PAD-X/Y 是否不同?

python - 为什么我的 Tkinter 计算器会因这段代码而崩溃?

python - 设置 tkinter.ttk.Treeview 列中文本的格式

linux - 通过 MS Access 连接到共享文件夹中的数据库