In [47]:
from sqlstring.sql_builder import SelectBuilder
In [48]:
builder = SelectBuilder()
builder.from_table('address').get_query_string()
Out[48]:
In [49]:
builder = SelectBuilder()
builder.from_table('address').distinct().get_query_string()
Out[49]:
In [50]:
builder = SelectBuilder()
builder.from_table('address').column(['city', 'state_code'])
builder.get_query_string()
Out[50]:
In [51]:
builder = SelectBuilder()
builder.from_table('address').where('state_code', '=', " 'CA' ")
builder.where('city', '=', " 'Oakland' ", 'AND')
builder.get_query_string()
Out[51]:
In [52]:
builder = SelectBuilder()
builder.from_table('address').group_by(['state_code', 'city'])
builder.get_query_string()
Out[52]:
In [53]:
builder = SelectBuilder()
builder.from_table('address').order_by(['state_code', 'city'], 'DESC')
builder.get_query_string()
Out[53]:
In [54]:
builder = SelectBuilder()
builder.from_table('address').limit(100).offset(200).get_query_string()
Out[54]:
In [55]:
builder = SelectBuilder()
builder.from_table('address').join_table('contact')
builder.where('address.state_code', '=', " contact.state_code ")
builder.get_query_string()
Out[55]:
In [67]:
def select_like_many():
word_list = ['json', 'query']
not_list = ['javascript', 'jquery']
builder = SelectBuilder()
builder.from_table('pypi_list').column(['package_name','package_description'])
for column in word_list:
value_string = " '%{0}%' ".format(column)
builder.where('package_description', 'LIKE ', value_string, 'AND')
for column in not_list:
value_string = " '%{0}%'".format(column)
builder.where('package_description', 'NOT LIKE ', value_string, 'AND')
return(builder.get_query_string(True)) # format sql
In [70]:
query_result = select_like_many()
print(query_result)
In [58]:
from sqlstring.sql_builder import InsertBuilder
In [59]:
builder = InsertBuilder()
builder.into_table('address').column(['city', 'state_code']).get_query_string()
Out[59]:
In [60]:
builder = InsertBuilder()
builder.into_table('address').column(['city', 'state_code'])
builder.get_query_string('OR REPLACE')
Out[60]:
In [61]:
from sqlstring.sql_builder import UpdateBuilder
In [62]:
builder = UpdateBuilder()
builder.update_table('address').set('state_name', " 'California' ")
builder.get_query_string()
Out[62]:
In [63]:
builder = UpdateBuilder()
builder.update_table('address').set('state_name', " 'California' ")
builder.where('state_code', '=', " 'CA' ").get_query_string()
Out[63]:
In [64]:
from sqlstring.sql_builder import DeleteBuilder
In [65]:
builder = DeleteBuilder()
builder.from_table('address').get_query_string()
Out[65]:
In [66]:
builder = DeleteBuilder()
builder.from_table('address').where('state_code', '=', " 'CA' ")
builder.where('city', '=', " 'Oakland' ", 'AND')
builder.get_query_string()
Out[66]: