Package: jetorm
JetORMThe simple wrapper for work with databases.Get started with JetORM!Connect with databaseConnecting to the database:MySQLAn instance of themysqlclass accepts2mandatoryand4optionalarguments as inputfromjetormimportmysqlmysql("host","database",port=3306,user="root",password="")For example (we have a local server and port 3306, database name - test):db=mysql("127.0.0.1","dbname")SQLiteAn instance of thesqliteclass accepts1mendotory argument as inputfromjetormimportsqlitedb=sqlite("dbfile.db")P.SIn the form of an instance name of a mysql or sqlite class, we will use the db nameMySQL CRUDCREATEIn order to create a table, there is methoddispenseExample:instance_of_the_mysql_class.dispense("table")This method create table and it's create default fieldidStructure of a tableIn order to create a table structure, there are several methods, we will analyze each.Method name:addThis method takes 4 arguments as input:table: str, name_of_the_field: str, type: str, unsigned=TrueJetORM MySQL Types:int(INT)varchar(VARCHAR)text(TEXT)dt:ct(datetime current timestamp)id(INDEX)bit(BIT)bool(BOOLEAN)tint(TINYINT)bint(BIGINT)Q:How to specify the field types correctly?A: A colon is placed after the type and the length of the values of this field is written. That is, if you have a VARCHAR field type, specify the length of values for it, for example, 255:db.add('table','name_of_the_field','varchar:255')Another Example:db.add('table','log_date','dt:ct')This code will create a field with thedatetimetype and the default value of current timestampExample:db.add('table','money','bint')This code will create a field with theBIGINTtypeExample:db.add('table','number','int:8')This code will create a field with anINTtype and a value length of 8Method name:putsThis method takes 4 arguments as input:table: str, column_values: dict, unsigned=TrueIt'screateandinsertthe data which you inputedFor example:db.puts('table',{'name':'Alex','age':18})So it will create fieldsnameandageand insert dataAlex, 18INSERTMethod name: insertThe method responsible for inserting values takes2arguments:table: str, column_values: dictFor example:db.insert('table',{'name':'John','age':25})Also there is method execute your sql:db.exec("YOUR SQL",values)For example:db.exec("SELECT * FROM `table` WHERE `password` =%s","qwerty123")Besides that, if the data is not recorded, use this method:db.store()READLoad the cellsTo get a cell from a table, you can use several methods:loadload_allreadread_onefindfind_oneMethod name:loadThis method returns a list with datadb.load(table:str,ids:tuple)For example:user=db.load('some_table',(1,3))Method name:load_allThis method returns all the cellsdb.load_all(table:str)For example:all_users=db.load_all('some_table')Method name:readThis method returns certain fieldsdb.read(table:str,fields:tuple,sub_sql:str=None,*values)For example:user=db.read('table',('name',),"`password` =%s",'qwerty')or like this:user=db.read('table',('age',))Method name:read_oneThis method is similar to thereadmethod, but it's adds to the SQL queryLIMIT 1Method name:findThis method returns celldb.find(table:str,sql:str,*values)For example:users=db.find('some_table',"`age` > 15")orusers=db.find('some_table',"`password` =%s",'qwerty')Method name:find_oneThis method is similar to thefindmethod, but it's adds to the SQL queryLIMIT 1Sorting methodsMethod name:recentThis method returns recent fielddb.recent(table:str,order_by_field:str)For example:recently_id_user=db.recent('table','id')Method name:countThis method returns number of entriesdb.count(table:str,sub_sql=None,*values)For example:how_many_users=db.count('table')or like this:user_exists=db.count('table','id =%s',5)Viewing tables in the consoleMethod name:viewFor example:two_users=db.view('table',(1,2))print(two_users)Method name:view_allSimilar in meaning to theload_allmethod
For example:all_users=db.view_all('table')print(all_users)UPDATEMethod name:updateThis method is similar in syntax to theputsmethod, but unlike it, this methodupdatesrecordsdb.update(table:str,column_values:dict,sub_sql=None,*values)For example:db.update('table',{'name':'Test'},"`id` =%s",10)Then, in the cell withid = 10, thenamefield for theTestwill be updatedor like this:db.update('table',{'name':'Test'})DELETEMethod name:trashThis method deletes an entrydb.trash(table:str,field_ids:tuple)For example:db.trash('table',(1,4,5))Then, in the cell withid = 1, 4, 5will be deleted.Method name:trash_sqlThis method is similar totrashmethoddb.trash_sql(table:str,sub_sql:str,*values)For example:db.trash_sql('table',"`id` < 5 OR `id` =%s",6)Then, in the cell withid < 5orid = 6will be deleted.Method name:wipeThis method completely removes all records from the tabledb.wipe(table:str)For example:db.wipe('table')Then, all the cells will be deleted.Method name:nukeThis method deletes the tabledb.nuke(table:str)For example:db.nuke('table')Then, the table will be deleted.SQLite CRUDUses the same methods as in MySQL CRUD (executes the same query), but the sturcture of the fields some differentStructure of a tableJetORM SQite Types:int(INT)varchar(VARCHAR)text(TEXT)dt:ct(datetime current timestamp)id(INDEX)bool(BOOLEAN)tint(TINYINT)bint(BIGINT)Unlike MySQL, the SQLite module does not have a BIT data type, soboolcan be used instead.CREATE, READ, UPDATE and DELETE methods SQLite module implements in the same way as the MySQL module.p.s Allmethodsand theirnames,argumentscompletelycoincidewith the mysql class.