i want to save a json array into azure sql server, but i am getting this error:
\serverCalls\sqlManager.py", line 95, in populateNewsLatest curse.execute(f""" pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Driver 17 for SQL Server]COUNT field incorrect or syntax error (0) (SQLExecDirectW)')
here is the json i want to save:
[
{
"source": {
"id": null,
"name": "Slashdot.org"
},
"author": "msmash",
"title": "World's Biggest Crypto Fortune Began With a Friendly Poker Game",
"description": "An anonymous reader shares a report: The Abu Dhabi Grand Prix draws princes, movie stars and world-famous athletes every year to party on Yas Island, the entertainment hub about 30 minutes from the center of downtown. Mingling among them last month was a figu\u2026",
"url": "https://slashdot.org/story/22/01/10/143214/worlds-biggest-crypto-fortune-began-with-a-friendly-poker-game",
"urlToImage": "https://a.fsdn.com/sd/topics/bitcoin_64.png",
"publishedAt": "2022-01-10T14:03:00Z",
"content": "The Abu Dhabi Grand Prix draws princes, movie stars and world-famous athletes every year to party on Yas Island, the entertainment hub about 30 minutes from the center of downtown. Mingling among the\u2026 [+1785 chars]"
}, ... ]
sqlmanager.py:
def populateNewsLatest(jsonpacket):
# print(jsonpacket)
with pyodbc.connect('DRIVER=' + driver +
';SERVER=tcp:' + server +
';PORT=1433;DATABASE=' +
database +
';UID=' + username + ';PWD=' + password) as conn:
conn.autocommit = True
with conn.cursor() as curse:
if curse.tables('newslatest').fetchone():
# delete if table exists
curse.execute('DROP TABLE [dbo].[newslatest]')
print('delete news latest/update')
# create new instance
curse.execute(
'CREATE TABLE newslatest(author varchar(28),content varchar(max),description varchar(max),publishedAt varchar(28),title varchar(28),url varchar(28),urlToImage varchar(28),id varchar(28),name varchar(28));')
json_string = json.dumps(jsonpacket)
print(json_string)
# dump json data to sql
curse.execute(f"""
DECLARE @json NVARCHAR(MAX)
SET @json=N'{json_string}';
INSERT INTO [dbo].[newslatest]
SELECT * FROM OPENJSON(@json) WITH(author varchar(28) '$.author',content nvarchar(max) '$.content',description nvarchar(max) '$.description',
publishedAt varchar(28) '$.publishedAt',title varchar(28) '$.title',url nvarchar(max) '$.url',
urlToImage nvarchar(max) '$.urlToImage',id varchar(28) '$.source.id',name varchar(28) '$.source.name');""")
return {"message": "successfully updated"}
else:
print('first time creation news latest')
curse.execute(
'CREATE TABLE newslatest(author varchar(28),content varchar(max),description varchar(max),publishedAt varchar(28),title varchar(28),url varchar(28),urlToImage varchar(28),id varchar(28),name varchar(28));')
json_string = json.dumps(jsonpacket)
curse.execute(f"""
DECLARE @json NVARCHAR(MAX)
SET @json=N'{json_string}';
INSERT INTO [dbo].[newslatest]
SELECT * FROM OPENJSON(@json) WITH(author varchar(28) '$.author',content nvarchar(max) '$.content',description nvarchar(max) '$.description',
publishedAt varchar(28) '$.publishedAt',title varchar(28) '$.title',url nvarchar(max) '$.url',
urlToImage nvarchar(max) '$.urlToImage',id varchar(28) '$.source.id',name varchar(28) '$.source.name');""")
return {"message": "successfully updated(first creation)"}