MHD011 commited on
Commit
a8212fd
·
verified ·
1 Parent(s): e9f978c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -25
app.py CHANGED
@@ -244,42 +244,25 @@ def handle_query():
244
  try:
245
  cursor = conn.cursor()
246
  cursor.execute(sql_query)
247
-
248
- # إذا كان الاستعلام لا يعيد أي أعمدة (مثل SELECT COUNT(*))
249
- if cursor.description is None:
250
- return jsonify({"data": []}), 200
251
-
252
  columns = [desc[0] for desc in cursor.description]
253
  rows = cursor.fetchall()
 
 
 
 
 
254
 
255
- # تحويل النتائج إلى JSON قابل للتسلسل
256
- result = []
257
- for row in rows:
258
- row_dict = {}
259
- for i, col in enumerate(columns):
260
- # معالجة أنواع البيانات غير القابلة للتسلسل مباشرةً
261
- value = row[i]
262
- if isinstance(value, (datetime.date, datetime.datetime)):
263
- value = value.isoformat()
264
- elif isinstance(value, decimal.Decimal):
265
- value = float(value)
266
- row_dict[col] = value
267
- result.append(row_dict)
268
 
269
- # إرجاع الرد مع ضمان تنسيق JSON صحيح
270
- response_data = {"data": result}
271
  return Response(
272
- json.dumps(response_data, ensure_ascii=False),
273
  status=200,
274
  mimetype='application/json; charset=utf-8'
275
  )
276
 
277
  except Exception as e:
278
  print(f"SQL execution error: {e}")
279
- return jsonify({
280
- "error": str(e),
281
- "generated_sql": sql_query
282
- }), 500
283
  finally:
284
  if cursor:
285
  cursor.close()
 
244
  try:
245
  cursor = conn.cursor()
246
  cursor.execute(sql_query)
 
 
 
 
 
247
  columns = [desc[0] for desc in cursor.description]
248
  rows = cursor.fetchall()
249
+ data = [dict(zip(columns, row)) for row in rows]
250
+
251
+ response_data = {
252
+ "data": data,
253
+ }
254
 
255
+ response_json = json.dumps(response_data, ensure_ascii=False)
 
 
 
 
 
 
 
 
 
 
 
 
256
 
 
 
257
  return Response(
258
+ response_json,
259
  status=200,
260
  mimetype='application/json; charset=utf-8'
261
  )
262
 
263
  except Exception as e:
264
  print(f"SQL execution error: {e}")
265
+ return jsonify({"error": str(e), "generated_sql": sql_query}), 500
 
 
 
266
  finally:
267
  if cursor:
268
  cursor.close()