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 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): 

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/", line 67, in application
response = frappe.handler.handle()
File "/home/reza/frappe-bench/apps/frappe/frappe/", line 77, in handle
File "/home/reza/frappe-bench/apps/frappe/frappe/", line 94, in execute_cmd
ret =, **frappe.form_dict)
File "/home/reza/frappe-bench/apps/frappe/frappe/", line 788, in call
return fn(*args, **newargs)
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/", line 63, in getdoctype
docs = get_meta_bundle(doctype)
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/", line 73, in get_meta_bundle
bundle = [frappe.desk.form.meta.get_meta(doctype)]
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/", line 22, in get_meta
meta = FormMeta(doctype)
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/", line 32, in __init__
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/", line 42, in load_assets
File "/home/reza/frappe-bench/apps/frappe/frappe/desk/form/", line 146, in load_templates
module = load_doctype_module(
File "/home/reza/frappe-bench/apps/frappe/frappe/modules/", 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/", line 590, in get_module
return importlib.import_module(modulename)
File "/usr/lib/python2.7/importlib/", line 37, in import_module
File "/home/reza/frappe-bench/apps/container_management/container_management/container_management/doctype/container/", 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"]

thank you for the nice explanation. :slight_smile: