Connecting to the database

I need help diagnosing a problem related to connecting to database in a Frappe app. In my app, i have created a doctype named Container. Than in the container.py file i have written codes to run a query. I have tried to connect to the related database with this command -

from __future__ import unicode_literals
import frappe
from frappe.model.document import Document
import datetime

class Container(Document): 
    pass

with frappe.db.connect("255a762c36") as connection:
    c = connection.cursor()

When i tried to access my doctype from Desk UI, it has thrown the following error-

Traceback (innermost last):
File "/home/reza/frappe-bench/apps/frappe/frappe/app.py", line 67, in application
response = frappe.handler.handle()
File "/home/reza/frappe-bench/apps/frappe/frappe/handler.py", line 77, in handle
execute_cmd(cmd)
File "/home/reza/frappe-bench/apps/frappe/frappe/handler.py", line 94, in execute_cmd
ret = frappe.call(method, **frappe.form_dict)
File "/home/reza/frappe-bench/apps/frappe/frappe/__init__.py", line 788, in call
return fn(*args, **newargs)
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/load.py", line 63, in getdoctype
docs = get_meta_bundle(doctype)
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/load.py", line 73, in get_meta_bundle
bundle = [frappe.desk.form.meta.get_meta(doctype)]
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/meta.py", line 22, in get_meta
meta = FormMeta(doctype)
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/meta.py", line 32, in __init__
self.load_assets()
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/meta.py", line 42, in load_assets
self.load_templates()
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/meta.py", line 146, in load_templates
module = load_doctype_module(self.name)
File "/home/reza/frappe-bench/apps/frappe/frappe/modules/__init__.py", line 64, in load_doctype_module
doctype_python_modules[key] = frappe.get_module(get_module_name(doctype, module, prefix))
File "/home/reza/frappe-bench/apps/frappe/frappe/__init__.py", line 590, in get_module
return importlib.import_module(modulename)
File "/usr/lib/python2.7/importlib/__init__.py", line 37, in import_module
__import__(name)
File "/home/reza/frappe-bench/apps/container_management/container_management/container_management/doctype/container/container.py", line 14, in 
with frappe.db.connect("255a762c36") as connection:
TypeError: connect() takes exactly 1 argument (2 given)

It seems i am passing 2 arguments, but i don’t understand how it is happening and how to solve it.

You don’t need to do this. use frappe.db.sql(“”" sql querry “”")

1 Like

@neilLasrado Thank you. I have thought about it earlier but couldn’t have gut to try that. :smiley:

@neilLasrado… i have a query though. If you don’t mind, i am asking for a suggestion. I have been trying to fetch data from the table via this command-

for row in frappe.db.sql("select x, y from tabTable_name")

Does that means i can catch the value of x and y by this-

row[0] = x
row[1] = y
frappe.get_all("Table_name", fields = ["x", "y"])
1 Like

will it return a list like [“x”, “y”] ?

It will return list of dictionary .

 rows = [{"x": x_value1, "y": y_value1}, ....., {"x": x_valuen, "y": y_valuen}]

To access

for row in frappe.get_all("Table_name", fields = ["x", "y"]):
   print row["x"]
   print row["y"]
3 Likes

thank you for the nice explanation. :slight_smile: