账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    python用pymssl连接数据的时候,怎么提前判定数据库是否开启?
    21
    0

    我用python连接数据库,当数据库的服务关闭的时候,程序会卡死。怎么提前判定数据是否开启?如果没有开启就返回值。

    试过用try except,但是好像没有用,login_timeout也用了感觉也不对。照样卡死

    相关代码

    import winsound
    import xlwt
    import pymssql
    import tkinter as tk
    window = tk.Tk()
    window.title('my window')
    window.geometry('300x300')
    e = tk.Entry(window, show=None)
    e.pack()
    def set_style(name, height, bold=False):

    style = xlwt.XFStyle()
    font = xlwt.Font()
    font.name = name
    font.bold = bold
    font.color_index = 4
    font.height = height
    style.font = font
    return style

    def write_excel(d):

    f = xlwt.Workbook()
    sheet1 = f.add_sheet('学生',cell_overwrite_ok=True)
    row0 = ["id","username","age"]
    #写第一行
    for i in range(0,len(row0)):
        sheet1.write(0,i,row0[i],set_style('Times New Roman',220,True))
    #从第二行开始写从数据库里面捞出来的数据
    for i in range(0,len(d)):
        for m in range(0, len(d[i])):
            sheet1.write(i + 1, m, d[i][m], set_style('Times New Roman', 220, True))
    f.save('F:/'+tt+'.xls',)

    global Mes1
    Mes1 = None
    def insert_point():

    conn = pymssql.connect(host="ddd", user='sa', password='ddd', database='master',login_timeout=10)
    try:
        cur = conn.cursor()
        global tt
        tt=e.get()
        if e.get()=='':
            show_eff(False)
        else:
            cur.execute('select * from temp_ross where id=%s', (e.get()))
            data = cur.fetchall()
            if len(data) !=0:
                t = []
                d = []
                for i in range(len(data)):
                    t.append(data[i][0])
                    t.append(data[i][1].rstrip())
                    t.append(data[i][2])
                    d.append(t)
                    t = []
                write_excel(d)
                cur.close()
                show_eff(True)
            else:
                show_eff(False)
    except pymssql.OperationalError:
        show_eff(False)

    def show_eff(e):

    global Mes1
    if Mes1 is None:
        Mes1 = tk.Message(text = 'OK' if e else 'Fail', width = 60)
        Mes1.pack()
    else:
        Mes1.pack_forget()
        Mes1 = tk.Message(text = 'OK' if e else 'Fail', width = 60)
        Mes1.pack()
    if e==False:
        winsound.Beep(900, 1000)

    def windowset():

    b1 = tk.Button(window, text='insert point', width=15,
                   height=2, command=insert_point)
    b1.pack()
    window.mainloop()

    windowset()

    能否有方法判断出数据库是否开启

    0
    打赏
    收藏
    点击回答
    您的回答被采纳后将获得:提问者悬赏的 10 元积分
        全部回答
    • 0
    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部