python,用sqllite3如何连接数据库

2025-05-02 03:10:50
推荐回答(1个)
回答1:

def getConnection(path):
    if not os.path.exists(path):
        return None
    try:
        connect = sqlite3.connect(path)
    except Exception, e:
        print "ConnectSubLibraryDB Error! :%s" % e
        return None
    return connect
conn = getConnection('xxxxx')#你的db文件路径
cur = conn.cursor()
ret = None
try:
    cur.execute(sqlStr)#执行的sql语句
    ret = cur.fetchall()
except Exception, e:
    print'_execSql Error:[%s], Reason:[%s]'%(sqlStr, e)
    ret = None
finally:       
    cur.close()
    conn.close()