Frappe.db.sql with empty result

hello
I am trying to get count(column ) to variable from database in server script
If there is no data in table error message showing to me

how I can check if there is no data in table I will make my variable equal 1

my code

parentaccount=doc.parent_account
lastinsertedaccountdate = frappe.db.sql("""
        SELECT  count(creation) FROM `tabAccount`
        WHERE parent_account = %s 
    """, parentaccount)[0][0] or 0

error message is
pymysql.err.DataError: (1264, “Out of range value for column ‘intcol’ at row 1”)

@Hamza_Erpnext try this
parentaccount = doc.parent_account
query_result = frappe.db.sql(“”"
SELECT COUNT(creation) FROM tabAccount
WHERE parent_account = %s
“”", parentaccount)
if query_result and query_result[0][0] is not None:
lastinsertedaccountdate = query_result[0][0]
else:
lastinsertedaccountdate = 1

1 Like

hello @Jeel
I try This code but still gave me error
I will attach picture for error and code
thanks.
erorr

Full console error:
request.js:457 Traceback (most recent call last):
File “apps/frappe/frappe/app.py”, line 95, in application
response = frappe.api.handle()
File “apps/frappe/frappe/api.py”, line 54, in handle
return frappe.handler.handle()
File “apps/frappe/frappe/handler.py”, line 47, in handle
data = execute_cmd(cmd)
File “apps/frappe/frappe/handler.py”, line 85, in execute_cmd
return frappe.call(method, **frappe.form_dict)
File “apps/frappe/frappe/init.py”, line 1622, in call
return fn(*args, **newargs)
File “apps/frappe/frappe/desk/form/save.py”, line 31, in savedocs
doc.save()
File “apps/frappe/frappe/model/document.py”, line 309, in save
return self._save(*args, **kwargs)
File “apps/frappe/frappe/model/document.py”, line 331, in _save
return self.insert()
File “apps/frappe/frappe/model/document.py”, line 273, in insert
self.db_insert(ignore_if_duplicate=ignore_if_duplicate)
File “apps/frappe/frappe/model/base_document.py”, line 517, in db_insert
frappe.db.sql(
File “apps/frappe/frappe/database/database.py”, line 220, in sql
self._cursor.execute(query, values)
File “env/lib/python3.10/site-packages/pymysql/cursors.py”, line 158, in execute
result = self._query(query)
File “env/lib/python3.10/site-packages/pymysql/cursors.py”, line 325, in _query
conn.query(q)
File “env/lib/python3.10/site-packages/pymysql/connections.py”, line 549, in query
self._affected_rows = self._read_query_result(unbuffered=unbuffered)
File “env/lib/python3.10/site-packages/pymysql/connections.py”, line 779, in _read_query_result
result.read()
File “env/lib/python3.10/site-packages/pymysql/connections.py”, line 1157, in read
first_packet = self.connection._read_packet()
File “env/lib/python3.10/site-packages/pymysql/connections.py”, line 729, in _read_packet
packet.raise_for_error()
File “env/lib/python3.10/site-packages/pymysql/protocol.py”, line 221, in raise_for_error
err.raise_mysql_exception(self._data)
File “env/lib/python3.10/site-packages/pymysql/err.py”, line 143, in raise_mysql_exception
raise errorclass(errno, errval)
pymysql.err.DataError: (1264, “Out of range value for column ‘intcol’ at row 1”)

parentaccount = doc.parent_account
try:
    lastinsertedaccountdate = frappe.db.sql("""
        SELECT  count(creation) FROM `tabAccount`
        WHERE parent_account = %s 
    """, parentaccount)[0][0]
except IndexError:
    lastinsertedaccountdate = 1

try this

1 Like

hi @manal_erpnext

I try it but not working still same error

clear the cache and restart the system

1 Like

Is that code in server script
because I need to make the code in server script

Thanks It’s Resolve

I think this problem is in my site
I try code in other site and it works good

Thank you all.