コンテンツにスキップ

PythonでSQL

ipynbFile pymssql__PythonでSQL.ipynb

In [1]:

1
2
3
4
5
import pymssql
import json

with open("account.json",'r') as f:
    data = json.load(f)

In [7]:

1
2
3
4
5
6
7
8
conn = pymssql.connect(data['server'],data['name'],data['password'],data['database'])

with conn.cursor(as_dict=True) as cursor:
    cursor.execute('select * from sampleTable')
    result = cursor.fetchall()

for i in result:
    print(i)

Success

1
2
3
4
5
{'ID': 1, 'Name': 'hoge      ', 'Value': 10.0}
{'ID': 2, 'Name': 'hoge      ', 'Value': None}
{'ID': 3, 'Name': 'hoge      ', 'Value': None}
{'ID': 4, 'Name': 'hoge      ', 'Value': None}
{'ID': 6, 'Name': 'HOGE      ', 'Value': None}

In [4]:

1
2
3
4
5
conn = pymssql.connect(data['server'],data['name'],data['password'],data['database'])

with conn.cursor(as_dict=True) as cursor:
    cursor.execute("INSERT INTO sampleTable (ID,Name) VALUES (6,'HOGE')")
    conn.commit()

最終更新日: 2022-07-20 14:47:11