1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import pyodbc
accdb_dir = 'C:/Users/xxx/Documents/Database1.accdb'
create_table_sql = '''\ create table suanfa ( id autoincrement primary key, value1 varchar(255), value2 varchar(255), result varchar(255) ) '''
select_sql = 'select * from suanfa where id>0'
insert_sql = 'insert into suanfa(value1, value2, result) values (?, ?, ?)'
update_sql = 'update suanfa(result,) values (?,)'
connection = pyodbc.connect(rf'Driver={{Microsoft Access Driver (*.mdb, *.accdb)}};DBQ={accdb_dir};') connection.autocommit = True
with connection.cursor() as cursor: cursor.execute(create_table_sql)
with connection.cursor() as cursor: for i in range(10): cursor.execute(insert_sql, i, i, i+1)
with connection.cursor() as cursor: cursor.execute(select_sql) values = cursor.fetchall() print(values)
aa, bb = values[0], values[1] rst = aa * bb cursor.execute(update_table_sql, rst)
for row in cursor.execute(select_sql): print(row)
|