Last active
July 6, 2022 17:33
Revisions
-
Razikus revised this gist
Jul 6, 2022 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -24,6 +24,12 @@ def insertNewRecord(self, value: str): query = f"INSERT INTO example_table (exampleField) VALUES(@what)" return self.client.sqlExec(query, {"what": value}) def getRecordById(self, id: int): with self: query = f"SELECT id, exampleField from example_table WHERE id = @id" return self.client.sqlQuery(query, {"id": id}) def createTables(self): with self: @@ -40,4 +46,8 @@ def createTables(self): example = ExampleClient("localhost:3322", "immudb", "immudb", "defaultdb") example.createTables() returned = example.insertNewRecord("test") print("Transaction number", returned.txs[0].header.id) queried = example.getRecordById(1) if(len(queried) > 0): idOf, valueOf = queried[0] print(f"Found record with id '{idOf}' and value '{valueOf}'") -
Razikus created this gist
Jul 6, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,43 @@ from immudb import ImmudbClient import time class ExampleClient: def __init__(self, immudbUrl, login, password, database): self.immudbUrl = immudbUrl self.login = login self.password = password self.database = database self.client = ImmudbClient(immudbUrl) self.lastLogin = -1 def __enter__(self): if(self.lastLogin + 60 * 10 < time.time()): self.client.login(self.login, self.password, self.database) self.lastLogin = time.time() return self def __exit__(self, exc_type, exc_val, exc_tb): pass def insertNewRecord(self, value: str): with self: query = f"INSERT INTO example_table (exampleField) VALUES(@what)" return self.client.sqlExec(query, {"what": value}) def createTables(self): with self: query = f"""CREATE TABLE IF NOT EXISTS example_table ( id INTEGER AUTO_INCREMENT, exampleField VARCHAR[60], PRIMARY KEY (id) ); CREATE INDEX IF NOT EXISTS on example_table (exampleField);""" self.client.sqlExec(query, dict()) if __name__ == "__main__": example = ExampleClient("localhost:3322", "immudb", "immudb", "defaultdb") example.createTables() returned = example.insertNewRecord("test") print("Transaction number", returned.txs[0].header.id)