历史评论归档

使用问题 · 172 次浏览
197906+abcd张三 创建于 2021-03-27 00:31
~历史讨论归档。~

回复内容
michaelqq 2021-06-19 21:02
#1

#!/usr/bin/env python

# -*- coding: UTF-8 -*-

import sqlite3



#"*.mdb, *.accdb"中间的空格不能少,少了就不识别,真是一个空格都不能少啊

def show_tables(chk='none'):

    '''显示mdb数据库的所有表'''

    conn = pyodbc.connect('Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=d:/mdb/tb.mdb', autocommit=False)

    cur = conn.cursor()

    tb = []

    chk = chk

    for i in cur.tables(tableType='table'):

        print(i)

        if i[2].find(chk) >= 0:

            tb.append(i[2])

    print(tb)

    cur.close()

    conn.close()

    return tb

def drivers():

    '''显示pyodbc支持的所有Drivers'''

    for i in pyodbc.drivers():

        print(i)

def sqlite_02():

    conn2 = sqlite3.connect('e:/files.db')

    conn3 = sqlite3.connect('e:/files.db', timeout=5)

    cur3 = conn3.cursor()

    cur2 = conn2.cursor()

    sql = '''CREATE TABLE if not exists t2(

            id integer PRIMARY KEY,

            ddd varchar(50)

            ) '''


    cur2.execute(sql)

    sql_in = "insert into t2('ddd') values (?)"

    cur2.execute(sql_in,['aa'])

    conn2.commit()

    cur3.execute(sql_in,['dd'])

    cur2.execute('select * from t2')

    print(cur2.fetchall())

    dd = cur2.execute("SELECT date('now')").fetchall()[0][0]

    print(dd)


    conn3.commit()

    conn3.close()

    conn2.commit()

    conn2.close()


def sqlite_22():

    conn2 = sqlite3.connect('e:/files2.db')

    cur2 = conn2.cursor()

    sql = '''CREATE TABLE if not exists t2(

            id integer PRIMARY KEY,

            ddd varchar(50)

            ) '''


    cur2.execute(sql)

    conn2.execute('attach database "e:\\files.db" as d;')

    sql_in = "insert into t2('ddd') values (?)"

    cur2.execute(sql_in,['aa'])

    conn2.commit()



    cur2.execute('select 2,* from t2 union all select 8,* from d.t2')

    print(cur2.fetchall())

    dd = cur2.execute("SELECT date('now')").fetchall()[0][0]

    print(dd)



    conn2.commit()

    conn2.execute('detach d')

    conn2.close()


def sqlite_03():


    f = r'e:\files.db'

    d = f

    db_name = d

    print(f,d,db_name)

    print(type(d))

    conn_num = 5

    conns = []

    for i in range(0, conn_num):

        try:

            conn = sqlite3.connect(db_name)

            conns.append(conn)

        except Exception as e:

            print('connect failed at %d' % i)


    print(len(conns))

    print(db_name)


if __name__ == '__main__':

    sqlite_22()

    #http://cn.voidcc.com/question/p-dpdhfyha-np.html


回复主贴