Skip to main content

Sqlite3 helper

Project description

DevSqlite3

A python package to create sqlite3 database with python class.

install by PyPI:

pip3 install DevSqlite3
or
python -m pip install DevSqlite3

if you have old version:

pip3 install --upgrade DevSqlite3
python -m pip install --upgrade DevSqlite3

setup sqlite3 database

Note: You can add field and remove it from your class, it's will automatically removed or added from sqlite3 database

also you can use:

1- dropColumnNotExists=False or True # default True 2- addColumnNotExists=False or True # default True

that's mean when your remove some fields from your class, it's will removed from database table also when adding fields to your class, it's will added to database

to use:

@Database('DatabaseFileName', path='folderName', dropColumnNotExists=False, addColumnNotExists=False)

Note: path, dropColumnNotExists, addColumnNotExists is not requires.

create sqlite3 database:

from DevSqlite3.core import Database, Table


@Database('DatabaseFileName', path='folderName')
class Users(Table):
	id = Table.integerField(primary=True, null=False) # id integer primary key not null
	username = Table.stringField() # username text, if you want to add not null Table.stringField(null=False)
	password = Table.stringField()
	join_time = Table.dateField()
	nicks = Table.listField()
	owned_item = table.dictField()
	# if you want to ignore column, make variable name starts with __, for example
	__ignore__ = "any"
	

you can also add custom function like Dao in room database:

@Database('DatabaseFileName', path='folderName')
class Users(Table):
	id = Table.integerField(primary=True, null=False) # id integer primary key not null
	username = Table.stringField() # username text, if you want to add not null Table.stringField(null=False)
	password = Table.stringField()
	join_time = Table.dateField()
	nicks = Table.listField()
	owned_item = table.dictField()
	
	def getUsers(self):
        return self.execute("select * from User").all()  # return list of class Users if detected else empty list

    def getUserById(self, i):
        return self.execute("select * from User where id=:id",
                            args={"id": i}).first()  # return Users class if detected else None

    def updatePassword(self, username, password):
        return self.execute("update User set password=:password where username=:username",
                            args={"username": username, "password": password}).run()  # return None if update or last row id if insert

getting data from database

u = Users()
users = u.getUsers()
for user in users:
	print(user.id, user.username, user.join_time)
	
	# update all
	
	user.password = "123"
	user.save()

user = u.getUserById(1)
if user:
	# delete
	user.delete()
	
	# or update
	user.password = "123"
	user.nicks = ["1", "2", "3"]
	user.owned_item = {"colors": ["red", "white", "etc"]}
	user.save()

insert data to database

from datetime import datetime

u = Users()
u.username = "omar.othman"
u.password = "password"
u.join_time = datetime.now()
u.save()

are you noob with sqlite commands? don't worry we can help you

u = Users()
find = u.where('username').equals('omar.othman').andWhere('password').equals('password').first() # select * from Users where username='omar.othman' and password='password'
if find:
	find.password = "newPassword"
	find.save() # update
else:
	u.username = "omar.othman"
	u.password = "newPassword"
	u.save() # insert

aslo you can add more tables to database

@Database('DatabaseFileName', path='folderName') # The database file name must be the same as the other name, or a new one will be created
class AnotherTableName(Table):
	# etc ...

python version requires >=2

update info

  • remove superclass from custom class table
  • change 'get_all' to 'all'
  • change 'get_first' to 'first'
  • added 'run' function

next update?

  • support MySql database!!!

Donate: https://paypal.me/nxdev

Project details


Release history Release notifications | RSS feed

This version

1.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

DevSqlite3-1.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

DevSqlite3-1.2-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file DevSqlite3-1.2.tar.gz.

File metadata

  • Download URL: DevSqlite3-1.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for DevSqlite3-1.2.tar.gz
Algorithm Hash digest
SHA256 3c028c64691d11844c14dfd19b2a0564c61e78e2a1abc58e184f5488cc79f008
MD5 78bcd225793bb7d9f4b3394fbb518caf
BLAKE2b-256 53bbbbbfd0a4ea8e0adc500aaa93d65d7b34d41e1c3751511ae0bdfd3e803294

See more details on using hashes here.

File details

Details for the file DevSqlite3-1.2-py3-none-any.whl.

File metadata

  • Download URL: DevSqlite3-1.2-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.5.0 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.6

File hashes

Hashes for DevSqlite3-1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 6ed91b8e9b91351f3ed000aad997b75e6794c24e8d3e9a77385821e8ca5fa2a3
MD5 90c534791c2f2f4299f29934e42ede29
BLAKE2b-256 f2feb90f9672b2019eed97d87a9d967149abba2b5a8067cb16fc68f742dbcee8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page