Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update examples #75

Merged
merged 4 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 34 additions & 11 deletions examples/working_with_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,44 @@

con = mindsdb_sdk.connect()

# get user's database (connected to mindsdb as rental_db)
db = con.databases.rental_db
# connect to mindsdb example database
example_db = con.databases.create(
'example_db',
engine='postgres',
connection_args={
"user": "demo_user",
"password": "demo_password",
"host": "3.220.66.106",
"port": "5432",
"database": "demo"
}
)

# get table
table1 = db.tables.house_sales
# connect to the empty user database
my_db = con.databases.create(
'my_db',
engine='postgres',
connection_args={
"user": "postgres",
"host": "localhost",
"port": "5432",
"database": "my_database"
}
)

# get home_rentals table
table1 = example_db.tables.get('demo_data.home_rentals')

# ---- create new table ----

# copy create table house_sales and fill it with rows with type=house
table2 = db.tables.create('house_sales2', table1.filter(type='house'))
# create table home_rentals in user db and fill it with rows with location=great
table2 = my_db.tables.create('home_rentals', table1.filter(location='great'))


# create table from csv file

df = pd.read_csv('my_data.csv')
table3 = db.tables.create('my_table', df)
table3 = my_db.tables.create('my_table', df)


# ---- insert into table ----
Expand All @@ -28,16 +51,16 @@

# ---- update data in table ----

# get all rows with type=house from table1 and update values in table2 using key ('saledate', 'type', 'bedrooms')
# get all rows with number_of_rooms=1 from table1 and update values in table2 using key ('location', 'neighborhood')
table2.update(
table1.filter(type='house'),
on=['saledate', 'type', 'bedrooms']
table1.filter(number_of_rooms=1),
on=['location', 'neighborhood']
)


# ---- delete rows from table ----

# delete all rows where bedrooms=2
table2.delete(bedrooms=2)
table2.delete(number_of_rooms=1)


8 changes: 6 additions & 2 deletions mindsdb_sdk/databases.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import List
from typing import List, Union

from mindsdb_sql.parser.dialects.mindsdb import CreateDatabase
from mindsdb_sql.parser.ast import DropDatabase, Identifier
Expand All @@ -7,6 +7,7 @@

from .query import Query
from .tables import Tables
from .handlers import Handler


class Database:
Expand Down Expand Up @@ -93,7 +94,7 @@ def list(self) -> List[Database]:
"""
return [Database(self, name) for name in self._list_databases()]

def create(self, name: str, engine: str, connection_args: dict) -> Database:
def create(self, name: str, engine: Union[str, Handler], connection_args: dict) -> Database:
"""
Create new integration and return it

Expand All @@ -102,6 +103,9 @@ def create(self, name: str, engine: str, connection_args: dict) -> Database:
:param connection_args: {"key": "value"} object with the connection parameters specific for each engine
:return: created Database object
"""
if isinstance(engine, Handler):
engine = engine.name

ast_query = CreateDatabase(
name=Identifier(name),
engine=engine,
Expand Down
4 changes: 2 additions & 2 deletions tests/test_sdk.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,9 +670,9 @@ def test_flow(self, mock_post, mock_put):
con.ml_engines.create(
'openai1',
openai_handler,
connection_data={'api_key': '111'}
connection_data={'api_key': 'sk-11'}
)
check_sql_call(mock_post, 'CREATE ML_ENGINE openai1 FROM openai USING api_key = "111"')
check_sql_call(mock_post, 'CREATE ML_ENGINE openai1 FROM openai USING api_key = \'sk-11\'')

con.ml_engines.create(
'openai1',
Expand Down