{"id":21528269,"url":"https://github.com/hrassi/sqlitestarter","last_synced_at":"2025-03-17T18:44:49.391Z","repository":{"id":220785270,"uuid":"752580813","full_name":"hrassi/sqlitestarter","owner":"hrassi","description":null,"archived":false,"fork":false,"pushed_at":"2024-02-04T09:23:05.000Z","size":5,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-24T06:09:44.434Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hrassi.png","metadata":{"files":{"readme":"Readme.txt","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-02-04T09:07:39.000Z","updated_at":"2024-02-04T09:11:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"f6f751d8-bb94-4890-92af-1ebdeccdbbf4","html_url":"https://github.com/hrassi/sqlitestarter","commit_stats":null,"previous_names":["hrassi/sqlitestarter"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2Fsqlitestarter","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2Fsqlitestarter/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2Fsqlitestarter/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hrassi%2Fsqlitestarter/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hrassi","download_url":"https://codeload.github.com/hrassi/sqlitestarter/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244091767,"owners_count":20396667,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-11-24T01:52:26.123Z","updated_at":"2025-03-17T18:44:49.366Z","avatar_url":"https://github.com/hrassi.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Basic function to work with sqlite3 with python\n- create file .db\n- create table with primary unique index in the db file\n- create def functions for:\n        search by last name with case sensitive and partial search\n        insert new row with auto index increment\n        update an entry\n        remove a row\n        print all the table\n\n*************************************************************************\n\npython Sqlite3\n\nimport sqlite3\n\nconn = sqlite3.connect (‘employee.db’) \n# this will create an employee.db file if it doesnt exist\n# or connect to it if it exist .\n# if u put instead ( ‘:memory:’ ) will create the file in the memory\n\n# then we should create a cursor to execute sql comand\n# to do so we put conn.cursor() into a variable c :\n\nc = conn.cursor()\n\n# now that we have a cursor, we will create a table named \n# employees inside the employee.db file with 4 columns :\n# id as int primary key, first name as text, last as text and pay as integer\n\nc.execute (“”” CREATE TABLE employees (\n                       id INTEGER PRIMARY KEY AUTOINCREMENT ,\n                       first  text ,\n  \t\t\tlast  text ,\n\t\t\tpay integer\n\t\t\t) “”” )\n\n# now we should commit our changes with :\n\nconn.commit()\n\n# then closing back the connection :\n\nconn.close()\n\n\n# to insert data in the table employees in the file employee.db :\n\nconn = sqlite3.connect (‘employee.db’)\nc = conn.cursor()\nc.execute(“INSERT INTO employees VALUES (1, ‘corey’ , ‘schafer, , 5000)”)\nconn.commit()\nconn.close()\n\n# example to insert from variables :\nc.execute(“INSERT INTO employees VALUES (?, ? , ?, ,?)” , ( var.id , var.first , var.last , var.pay))\n### or : \nc.execute(“INSERT INTO employees VALUES (:id ,:first ,:last, ,:pay)” ,{‘id’:var, ‘first’: var1,’last’:var2…..}\n\n\n# to select  a row in the employees table :\n\nc.execute(“SELECT * FROM employees WHERE last = ’schafer’”)\nprint(c.fetchone())\n# (1,'corey',schafer',5000)\n# to return the search inside a list use fetchall:\nprint(c.fetchall())\n# [(1,'corey','schafer',5000),(2,'sdada','dsfss','gtret')]\n\n# to create table with unique index:\n\nc.execute (\"\"\" CREATE TABLE employees (\n                       id INTEGER PRIMARY KEY AUTOINCREMENT ,\n                       first  text ,\n  \t\t\tlast  text ,\n\t\t\tpay integer\n\t\t\t) \"\"\" )\n\n\n\n\n\ndef insert_emp(emp):\n    with conn:\n        c.execute(\"INSERT INTO employees VALUES(?,?,?,?)”, (emp))\n\n\ndef get_emp_by_name(name):\n    name='%'+name.lower()+'%' #transform to lower case and partial search\n    c.execute(\"SELECT * FROM employees  WHERE LOWER(last) LIKE :last\"  ,{'last':name })\n    return (c.fetchall())\n\ndef update_pay(emp,pay):\n    print (emp[0])\n    print(pay)\n    with conn:\n        c.execute(\"\"\"UPDATE employees SET pay = :pay\n                    WHERE first = :first AND last = :last\"\"\",\n                  {'first': emp[0], 'last': emp[1], 'pay': pay})\n\ndef remove_emp(emp):\n    print(emp[1])\n    with conn:\n        c.execute(\"DELETE from employees WHERE first = :first AND last = :last\",\n                  {'first':emp[0],'last':emp[1]})\n\n### to get the next auto id :\nc.execute(\"SELECT COUNT(*) FROM employees \")\nrow_nmbr=((c.fetchall()))\nnext_id= ((row_nmbr[(0)])[0])+2\nprint(next_id)\n\n\n\n\n### inserting using the def function below where next_id is autoindex:\ninsert_emp([next_id,’chakib','touma',2400])\n\n### updating pay column of emp_1 using def function at the beginning\nupdate_pay(['Houssam','Rassi'], 50000)\n\n### remove employee from database using first and last name in emp\nremove_emp(['corey','shafer'])\n### select a row in table using name(last name) with def fonction at the beginning\nemp=get_emp_by_name(\"Rassi\")\nprint (emp)\n\n\n### to print all the employee table :\nc.execute(\"select * from employees\")\nall_rows = c.fetchall()\nfor row in all_rows:\n    print (row)\n\n\nconn.commit()\nconn.close()\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrassi%2Fsqlitestarter","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhrassi%2Fsqlitestarter","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhrassi%2Fsqlitestarter/lists"}