Skip to content

Instantly share code, notes, and snippets.

@BlogBlocks
Created September 4, 2018 07:59
Show Gist options
  • Save BlogBlocks/cf4ebc768b16b64d2a8d8c361cc43416 to your computer and use it in GitHub Desktop.
Save BlogBlocks/cf4ebc768b16b64d2a8d8c361cc43416 to your computer and use it in GitHub Desktop.
Hand Little CRUD sqlite database tool
#!/usr/local/bin/python
import sys
import sqlite3
conn = sqlite3.connect("~/NED.db")
conn.text_factory = str
c = conn.cursor()
if len(sys.argv) < 3:
print "\n******* NED - Notes Editor **************"
print "Not enough options were passed."
print "NED requires 2 arguments. the first -H , -R , -I , -D or -S .\nThe second can be a period."
print "If printing the database -T also add a filename of your choice ( no quotes required ):"
print " Example: NED -T Data2Text.txt"
print "If wanting to read all entries use -R . (use the period)"
print "even use the period with help. -H . must be entered."
print "******************************************\n"
sys.exit()
mod = sys.argv[1]
def create():
import sqlite3
conn = sqlite3.connect("~/NED.db")
conn.text_factory = str
c = conn.cursor()
c.execute("CREATE VIRTUAL TABLE PROJECT using FTS4 (input)")
conn.commit()
text = "Database Created"
return text
def insert(data,conn=conn, c=c):
c.execute("INSERT into PROJECT values (?)", (data,))
for row in c.execute("SELECT ROWID,* FROM PROJECT ORDER BY ROWID DESC LIMIT 1"):
print "\nPOST VERIFIED:\n",row[0],row[1]
conn.commit()
conn.close()
return data
def search(data,conn=conn, c=c):
for row in c.execute("SELECT ROWID,* FROM PROJECT WHERE input MATCH ?",(data,)):
print "\nINFO Found Here:\n",row[0],row[1]
conn.commit()
conn.close()
return data
def delete(rowid,conn=conn, c=c):
c.execute("DELETE FROM PROJECT WHERE rowid = ?", (rowid,))
conn.commit()
conn.close()
text = "ROWID "+rowid+" Deleted"
return text
def main():
conn = sqlite3.connect("~/NED.db")
conn.text_factory = str
c = conn.cursor()
for row in c.execute("SELECT rowid, * FROM PROJECT"):
print row[0],": ",row[1]
def prtmain(filename):
fn = open(filename, "w")
conn = sqlite3.connect("~/NED.db")
conn.text_factory = str
c = conn.cursor()
for row in c.execute("SELECT rowid, * FROM PROJECT"):
TEXT = "id:"+str(row[0])+"\n"+str(row[1])
TEXT = str(TEXT)
TEXT = TEXT.replace('\\n','\n')
TEXT = "".join(TEXT)
fn.write(TEXT+'\n----\n')
def HELP():
TXT = """
USE: NED argv[1] argv[2]
argv[1] sets the mod:
-I insert / -D delete / -R read / -H help
examples:
Notice the entry is in parenthese.
-I to insert "STUFF to be inserted"
NED -I "STUFF to be inserted"
-D to delete where rowid is 3
NED -D 3
Notice the period after -R .
-R . read all
To search for the term "current project"
NED -S 3
-S "current project"
NED -R .
-H help on options
NED -H .
"""
print TXT
if mod == "-H" or mod == "h":
HELP()
if mod == "-R":
main()
if mod == "-I":
data = sys.argv[2]
insert(data)
if mod == "-D":
rowid = sys.argv[2]
delete(rowid)
if mod == "-S":
data = sys.argv[2]
search(data)
if mod == "-T":
filename = sys.argv[2]
prtmain(filename)
if mod == "-CREATE":
create()
print create
else:
print "_________________\n"
print sys.argv[2],"Command Completed"
@BlogBlocks
Copy link
Author

BlogBlocks commented Sep 4, 2018

I use this all the time and have for several years.
Look it over it has full CRUD ability and Plus a print database to text. The search uses full text.
You can remove the *py extension .
Then run chmod +x to make it executable. Save in the home directory and create a symlink to in /usr/local/bin
That way it can be run in any directory and any terminal. If you do that, the use is extremely easy.
EXAMPLES:
If you type: NED - I " The apostrophe holds the terminal open.
You may paste whole snippets and text in it and it will store keeping format.
HOWEVER the pasted text can not have an apostrophe unless it is escaped with a back slash.
To enter the info into the database type an apostrophe and press enter. Everything pasted in the terminal will be entered. Entries are printed and confirmed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment