Table Of Contents

Previous topic

db_schema — Database Schema Management Module

Next topic

page_body — Page Body Serverside Module

This Page

db — Database

database object — conn

Database(host='', user='', password='', use_default = 0):

Open a database connection with the given parmeters, if use_default is True, use the login details from defs.py. This is called by the request handler and is accessible using the conn global variable. the sql method is also global to run queries

db.host
Database host or ‘localhost’
db.user
Database user
db.password
Database password - cleared after connection is made
db.is_testing
1 if session is in Testing Mode else 0
db.in_transaction
1 if connection is in a Transaction else 0
db.testing_tables
list of tables, tables with tab + doctype
db.connect()
Connect to a database
db.use(db_name)
USE db_name
db.set_db(account)
Switch to database of given account
db.check_transaction_status(query)
Update in_transaction and check if “START TRANSACTION” is not called twice
db.fetch_as_dict()
Internal - get results as dictionary
db.sql(query, values=(), as_dict = 0, as_list = 0, allow_testing = 1)
  • Execute a query, with given values
  • returns as a dictionary if as_dict = 1
  • returns as a list of lists (with cleaned up dates and decimals) if as_list = 1
db.convert_to_lists(res)
Convert the given result set to a list of lists (with cleaned up dates and decimals)
db.replace_tab_by_test(query)
Relace all tab + doctype to test + doctype
db.get_testing_tables()
Get list of all tables for which tab is to be replaced by test before a query is executed
db.get_value(doctype, docname, fieldname)

Get a single value from a record.

For Single records, let docname be = None

db.get_description()
Get metadata of the last query
db.field_exists(dt, fn)
Returns True if fn exists in DocType dt
db.exists(dt, dn)
Returns true if the record exists
db.close()
Close my connection