! SimpleBase ! Aat Don @2015 HTML00$="" HTML00$=HTML00$+"" HTML00$=HTML00$+"" HTML08$=HTML08$+"" HTML09$="

" HTML09$=HTML09$+"" HTML09$=HTML09$+" " HTML09$=HTML09$+" " HTML09$=HTML09$+" " HTML09$=HTML09$+" " HTML09$=HTML09$+" " HTML09$=HTML09$+"" HTML09$=HTML09$+"
" HTML09$=HTML09$+"

" HTML09$=HTML09$+"" HTML09$=HTML09$+"" HTML09$=HTML09$+"

" HTML09$=HTML09$+"
" HTML09$=HTML09$+"
" HTML09$=HTML09$+"
" HTML09$=HTML09$+"Del -> Delete this row
" HTML09$=HTML09$+"Edit -> Edit this row, changes saved after Submit
" HTML09$=HTML09$+"Ins -> Insert row (toggle Below or Above this row)
" HTML09$=HTML09$+"" HTML09$=HTML09$+"" GR.OPEN 255,192,192,192,0,0 PAUSE 1000 GR.SCREEN w,h ScaleY=600 ScaleX=w/h*ScaleY sx=w/ScaleX sy=h/ScaleY GR.SCALE sx,sy WAKELOCK 3 DataPath$="../../SimpleBase/" DBPath$="../../SimpleBase/databases/" DIM Menu[5],ShowMenu[5] LIST.CREATE S,FndTbls Kap=0 DBOpen=0 NWCreated=0 GR.SET.STROKE 5 GR.TEXT.ALIGN 2 GR.TEXT.SIZE 17 GR.TEXT.BOLD 1 GR.TEXT.SETFONT "sans serif","B" GR.BITMAP.CREATE MenuButton,100,35 GR.BITMAP.DRAWINTO.START MenuButton GR.COLOR 255,255,0,0,0 GR.RECT g,3,3,97,32 GR.COLOR 255,255,255,0,1 GR.RECT g,3,3,97,32 GR.BITMAP.DRAWINTO.END GR.COLOR 255,0,0,0,1 FOR i=1 TO 3 READ.NEXT T$ GR.BITMAP.CREATE Menu,100,35 GR.BITMAP.DRAWINTO.START Menu GR.BITMAP.DRAW g,MenuButton,0,0 GR.TEXT.DRAW g,50,25,T$ GR.BITMAP.DRAWINTO.END GR.BITMAP.DRAW g,Menu,(i-1)*100,0 NEXT i FOR i=1 TO 5 READ.NEXT T$ GR.BITMAP.CREATE Menu[i],100,35 GR.BITMAP.DRAWINTO.START Menu[i] GR.BITMAP.DRAW g,MenuButton,0,0 GR.TEXT.DRAW g,50,25,T$ GR.BITMAP.DRAWINTO.END GR.BITMAP.DRAW ShowMenu[i],Menu[i],50,i*35 GR.HIDE ShowMenu[i] NEXT i GR.COLOR 255,255,255,255,1 GR.RECT g,600,5,800,30 GR.RECT g,600,50,800,100 GR.COLOR 255,0,0,0,0 GR.RECT g,600,5,800,30 GR.RECT g,600,50,800,100 GR.COLOR 255,0,0,0,1 GR.TEXT.DRAW DBCurName,700,23,"No DataBase" GR.TEXT.DRAW DBTables,700,70,"Contains 1 table(s)" GR.TEXT.DRAW g,700,90,"Tap here to view/edit" GR.BITMAP.CREATE P,210,60 GR.BITMAP.DRAWINTO.START P GR.COLOR 255,192,192,192,1 GR.RECT g,0,0,210,60 GR.BITMAP.DRAWINTO.END GR.BITMAP.DRAW Cover,P,595,45 GR.RENDER GR.SET.STROKE 2 GR.TEXT.SIZE 14 GR.BITMAP.CREATE ag,400,320 GR.BITMAP.DRAWINTO.START ag GR.COLOR 255,0,0,0,1 GR.RECT g,0,0,400,320 GR.COLOR 255,255,255,255,1 FOR i=1 TO 11 READ.NEXT T$ GR.TEXT.DRAW g,200,i*28,T$ NEXT i GR.BITMAP.DRAWINTO.END GR.BITMAP.DRAW About,ag,150,50 GR.HIDE About GR.COLOR 255,0,0,0,1 ShowTable=0 DO GOSUB GetTouch IF x<300 & y<35 THEN q=INT(x/100)+1 GOSUB q,SubMenu1,SubMenu2,SubMenu3 ENDIF IF (x<800 & x>600 & y>50 & y<100 & CurDB$<>"") | (NWCreated=1) THEN NWCreated=0 DIALOG.SELECT ShowTable,FndTbls,"Select Table to View/Edit" ENDIF IF ShowTable>0 THEN GOSUB DrawTable UNTIL Kap WAKELOCK 5 IF DBOpen THEN SQL.CLOSE DB_Ptr GR.CLOSE EXIT READ.DATA "DataBase","About","Quit","Load","Tables","New","Delete","RFO code" READ.DATA "About SimpleBase." READ.DATA "This app is meant to be a small aid in creating," READ.DATA "viewing and modifying RFO-Basic! DataBases." READ.DATA "It is not tested with large datasets, nor with" READ.DATA "DataBases from other origin. So no guarantees." READ.DATA "Remember any changes made to a table in view" READ.DATA "are directly saved to the DataBase !" READ.DATA "Columns can NOT be deleted !!!" READ.DATA "The demo Data delivered with this app are pure" READ.DATA "fantasy, so any resemblance to real people...." READ.DATA "Tap screen to continue....." SubMenu1: FOR i=1 TO 4 GR.SHOW ShowMenu[i] NEXT i IF CurDB$<>"" THEN GR.SHOW ShowMenu[5] GR.RENDER GOSUB GetTouch FOR i=1 TO 5 GR.HIDE ShowMenu[i] NEXT i GR.RENDER IF x<150 & x>50 & y<210 & y>35 THEN sq=INT(y/35) GOSUB sq,mnuLoad,mnuTables,mnuNew,mnuDelete IF sq=5 & CurDB$<>"" THEN GOSUB SubMenu4 ENDIF GR.RENDER RETURN mnuLoad: IF DBOpen THEN CurDB$="" ARRAY.DELETE FndTables$[] SQL.CLOSE DB_Ptr LIST.CLEAR FndTbls DBOpen=0 GR.SHOW Cover ENDIF LIST.CREATE S,FFound FILE.DIR DBPath$,FilesFound$[] ARRAY.LENGTH L,FilesFound$[] FOR i=1 TO L IF RIGHT$(FilesFound$[i],7)<>"journal" THEN LIST.ADD FFound,FilesFound$[i] ENDIF NEXT i DIALOG.SELECT q,FFound,"Select DataBase to LOAD" ARRAY.DELETE FilesFound$[] IF q=0 THEN LIST.CLEAR FFound RETURN ENDIF LIST.GET FFound,q,CurDB$ LIST.CLEAR FFound ! Use some raw queries to find out what this DBase looks like... SQL.OPEN DB_Ptr,DBPath$+CurDB$ DBOpen=1 SQL.RAW_QUERY Info,DB_Ptr,"SELECT name FROM sqlite_master WHERE type='table'" xdone=0 DO SQL.NEXT xdone,Info,TableNames$ IF !xdone & TableNames$<>"android_metadata" & TableNames$<>"sqlite_sequence" THEN LIST.ADD FndTbls,TableNames$ ENDIF UNTIL xdone LIST.SIZE FndTbls,NumOfTables DIM FndTables$[NumOfTables] ! Save table name(s) FOR j=1 TO NumOfTables LIST.GET FndTbls,j,TN$ FndTables$[j]=TN$ NEXT j FOR j=1 TO NumOfTables SQL.RAW_QUERY Info,DB_Ptr,"pragma table_info("+FndTables$[j]+")" SQL.QUERY.LENGTH Cols,Info FndTables$[j]=FndTables$[j]+","+INT$(Cols) FOR i=1 TO Cols SQL.NEXT xdone,Info,v$,Names$ FndTables$[j]=FndTables$[j]+","+Names$ NEXT i NEXT j ! Now we have all needed names in FndTables$[] GR.MODIFY DBCurName,"text",CurDB$ GR.MODIFY DBTables,"text","Contains "+INT$(NumOfTables)+" table(s)" GR.HIDE Cover GR.RENDER RETURN mnuTables: IF CurDB$<>"" THEN LIST.CREATE S,presentTables FOR i=1 TO NumOfTables LIST.ADD presentTables,WORD$(FndTables$[i],1,",") NEXT i LIST.ADD presentTables,"======= action =========" LIST.ADD presentTables,"ADD NEW TABLE" LIST.ADD presentTables,"DELETE TABLE" LIST.ADD presentTables,"======================" LIST.ADD presentTables,"Cancel" DIALOG.SELECT gq,presentTables,"These tables are present" FOR i=5 TO 1 STEP -1 LIST.REMOVE presentTables,NumOfTables+i NEXT i IF gq=NumOfTables+2 THEN DIM NwColumnNames$[50] INPUT "Enter name for new Table ",NwTableNames$,"",isCanceled IF isCanceled THEN F_N.BREAK NwTableNames$=REPLACE$(NwTableNames$," ","_") NwTableNames$=REPLACE$(NwTableNames$,",","_") INPUT "How many columns for Table "+NwTableNames$,NwCols,,isCanceled IF isCanceled THEN F_N.BREAK FOR j=1 TO NwCols INPUT "Enter name for Column "+INT$(j)+" in Table ",NwColumnNames$[j],"",isCanceled IF isCanceled THEN F_N.BREAK NwColumnNames$[j]=REPLACE$(NwColumnNames$[j]," ","_") NwColumnNames$[j]=REPLACE$(NwColumnNames$[j],",","_") NEXT j IF isCanceled THEN ARRAY.DELETE NwColumnNames$[] RETURN ENDIF FndTables$=NwTableNames$+","+INT$(NwCols+1)+",_id" FOR j=1 TO NwCols FndTables$=FndTables$+","+NwColumnNames$[j] NEXT j Create$="CREATE TABLE "+NwTableNames$+"( _id INTEGER PRIMARY KEY AUTOINCREMENT" FOR j=1 TO NwCols Create$=Create$+","+NwColumnNames$[j]+" TEXT" NEXT j Create$=Create$+")" SQL.EXEC DB_Ptr,Create$ ! Add 1 empty row SQL.INSERT DB_Ptr,NwTableNames$,NwColumnNames$[1],"" ! Add to end of array NumOfTables=NumOfTables+1 ARRAY.COPY FndTables$[],Temp$[1] ARRAY.DELETE FndTables$[] Temp$[NumOfTables]=FndTables$ ARRAY.COPY Temp$[],FndTables$[] ARRAY.DELETE Temp$[] ARRAY.DELETE NwColumnNames$[] LIST.ADD FndTbls,NwTableNames$ GR.MODIFY DBTables,"text","Contains "+INT$(NumOfTables)+" table(s)" ENDIF IF gq=NumOfTables+3 THEN DIALOG.SELECT gq,presentTables,"Select table to delete" IF gq>0 THEN SQL.DROP_TABLE DB_Ptr,WORD$(FndTables$[gq],1,",") FOR i=gq TO NumOfTables-1 FndTables$[i]=FndTables$[i+1] NEXT i NumOfTables=NumOfTables-1 LIST.CLEAR FndTbls FOR i=1 TO NumOfTables LIST.ADD FndTbls,WORD$(FndTables$[i],1,",") NEXT i ARRAY.COPY FndTables$[],Temp$[] ARRAY.DELETE FndTables$[] ARRAY.COPY Temp$[1,NumOfTables],FndTables$[] ARRAY.DELETE Temp$[] GR.MODIFY DBTables,"text","Contains "+INT$(NumOfTables)+" table(s)" ENDIF ENDIF ELSE POPUP "NO DataBase Loaded !",0,0,0 ENDIF RETURN mnuNew: INPUT "Enter a name for your DataBase",NwDBName$,"",isCanceled IF isCanceled THEN RETURN IF LOWER$(RIGHT$(NwDBName$,3))<>".db" THEN NwDBName$=NwDBName$+".db" FILE.EXISTS IsOld,DBPath$+NwDBName$ IF IsOld THEN DIALOG.MESSAGE NwDBName$+" already exists !","Overwrite this DataBase ?",Ask,"Yes","No" IF Ask=1 THEN FILE.DELETE q,DBPath$+NwDBName$ ELSE RETURN ENDIF ENDIF INPUT "How many tables do you want your DataBase to have ?",NwTables,,isCanceled IF isCanceled THEN RETURN DIM NwTableNames$[NwTables+1] DIM NwCols[NwTables] DIM NwColumnNames$[NwTables,50] FOR i=1 TO NwTables INPUT "Enter name for Table "+INT$(i),NwTableNames$[i],"",isCanceled IF isCanceled THEN F_N.BREAK NwTableNames$[i]=REPLACE$(NwTableNames$[i]," ","_") NwTableNames$[i]=REPLACE$(NwTableNames$[i],",","_") INPUT "How many columns for Table "+NwTableNames$[i],NwCols[i],,isCanceled IF isCanceled THEN F_N.BREAK FOR j=1 TO NwCols[i] INPUT "Enter name for Column "+INT$(j)+" in Table "+INT$(i),NwColumnNames$[i,j],"",isCanceled IF isCanceled THEN F_N.BREAK NwColumnNames$[i,j]=REPLACE$(NwColumnNames$[i,j]," ","_") NwColumnNames$[i,j]=REPLACE$(NwColumnNames$[i,j],",","_") NEXT j NEXT i IF isCanceled THEN RETURN NwTableNames$[NwTables+1]="OK, create DataBase !" DO DIALOG.SELECT Item,NwTableNames$[],"Tap on a table to review its column names" IF Item"" THEN ARRAY.DELETE FndTables$[] ENDIF DIM FndTables$[NwTables] FOR i=1 TO NwTables FndTables$[i]=NwTableNames$[i]+","+INT$(NwCols[i]+1)+",_id" FOR j=1 TO NwCols[i] FndTables$[i]=FndTables$[i]+","+NwColumnNames$[i,j] NEXT j NEXT i ! Make new DB current CurDB$=NwDBName$ LIST.CLEAR FndTbls NumOfTables=NwTables FOR i=1 TO NumOfTables LIST.ADD FndTbls,NwTableNames$[i] NEXT i ARRAY.DELETE NwTableNames$[] ARRAY.DELETE NwCols[] ARRAY.DELETE NwColumnNames$[] DBOpen=1 GR.MODIFY DBCurName,"text",CurDB$ GR.MODIFY DBTables,"text","Contains "+INT$(NumOfTables)+" table(s)" GR.HIDE Cover GR.RENDER NWCreated=1 RETURN mnuDelete: FILE.DIR DBPath$,FilesFound$[] DIALOG.SELECT q,FilesFound$[],"Select File to delete" IF q>0 THEN IF FilesFound$[q]<>CurDB$ THEN DIALOG.MESSAGE "ARE YOU SURE ?","Delete file "+FilesFound$[q]+" ?",Ask,"Yes","No" IF Ask=1 THEN FILE.DELETE qq,DBPath$+FilesFound$[q] ELSE POPUP "DataBase in use !",0,0,0 ENDIF ENDIF ARRAY.DELETE FilesFound$[] RETURN SubMenu2: GR.SHOW About GR.RENDER GOSUB GetTouch GR.HIDE About GR.RENDER RETURN SubMenu3: Kap=1 RETURN SubMenu4: IF IS_IN (".",CurDB$) THEN App$=LEFT$(CurDB$,IS_IN (".",CurDB$)-1)+".bas" ELSE App$=CurDB$+".bas" ENDIF DIALOG.MESSAGE "Generate RFO Basic! code needed to use this Database","Do you want to make "+App$+" ?",Q,"Yes","No" ! Create RFO Basic! code IF q=1 THEN TEXT.OPEN W,F1,DataPath$+App$ TEXT.WRITELN F1,"! App created with SimpleBase" TEXT.WRITELN F1,"! CHANGE DBPath$ AS NEEDED......" TEXT.WRITELN F1,"CONSOLE.TITLE "+CHR$(34)+"Generated DataBase Code"+CHR$(34) TEXT.WRITELN F1,"DBPath$="+CHR$(34)+"../../SimpleBase/databases/"+CHR$(34) TEXT.WRITELN F1,"DBName$=DBPath$+"+CHR$(34)+CurDB$+CHR$(34) FOR i=1 TO NumOfTables TEXT.WRITELN F1,"TableName"+INT$(i)+"$="+CHR$(34)+WORD$(FndTables$[i],1,",")+CHR$(34) FOR j=1 TO VAL(WORD$(FndTables$[i],2,",")) TEXT.WRITELN F1,"T"+INT$(i)+"C"+INT$(j)+"$="+CHR$(34)+WORD$(FndTables$[i],j+2,",")+CHR$(34) NEXT j NEXT i TEXT.WRITELN F1,"PRINT "+CHR$(34)+"Showing contents of "+CHR$(34)+"+ DBName$" TEXT.WRITELN F1,"SQL.OPEN DB_Ptr,DBName$" FOR i=1 TO NumOfTables TEXT.WRITELN F1,"! Query table "+WORD$(FndTables$[i],1,",") TEXT.WRITELN F1,"PRINT" TEXT.WRITELN F1,"PRINT "+CHR$(34)+"Table : "+WORD$(FndTables$[i],1,",")+CHR$(34) ColN$=CHR$(34)+"_id"+CHR$(34) FOR j=2 TO VAL(WORD$(FndTables$[i],2,",")) ColN$=ColN$+","+CHR$(34)+WORD$(FndTables$[i],j+2,",")+CHR$(34) NEXT j TEXT.WRITELN F1,"PRINT "+CHR$(34)+"Columns : "+CHR$(34)+ColN$ RCol$=CHR$(34)+"_id" FOR j=2 TO VAL(WORD$(FndTables$[i],2,",")) RCol$=RCol$+","+WORD$(FndTables$[i],j+2,",") NEXT j TEXT.WRITELN F1,"SQL.QUERY cursor,DB_Ptr,TableName"+INT$(i)+"$,"+RCol$+CHR$(34) TEXT.WRITELN F1,"DO" RCol$="" FOR j=1 TO VAL(WORD$(FndTables$[i],2,",")) RCol$=RCol$+","+"T"+INT$(i)+"C"+INT$(j)+"V$" NEXT j TEXT.WRITELN F1,"SQL.NEXT xdone,cursor"+RCol$ RCol$=RIGHT$(RCol$,LEN(RCol$)-1) TEXT.WRITELN F1,"IF !xdone THEN PRINT "+RCol$ TEXT.WRITELN F1,"UNTIL xdone" NEXT i TEXT.WRITELN F1,"SQL.CLOSE DB_Ptr" TEXT.WRITELN F1,"PRINT "+CHR$(34)+"--- end of data ---"+CHR$(34) TEXT.WRITELN F1,"END" TEXT.CLOSE F1 DIALOG.MESSAGE "SimpleBase",DataPath$+App$+" created !",q,"OK" ENDIF RETURN GetTouch: DO GR.TOUCH Touched,x,y UNTIL Touched DO GR.TOUCH Touched,x,y UNTIL !Touched x/=sx y/=sy RETURN DrawTable: TableNr=ShowTable ShowTable=0 TableName$=WORD$(FndTables$[TableNr],1,",") NumOfColumns=VAL(WORD$(FndTables$[TableNr],2,",")) GOSUB MakeTable HTML.OPEN HTML.ORIENTATION 0 HTML.CLEAR.CACHE HTML.LOAD.STRING HTML$ PAUSE 500 ! Create n cursors DIM Info[NumOfColumns] Columns$="_id" SQL.QUERY Info[1],DB_Ptr,TableName$,Columns$ SQL.QUERY.LENGTH L,Info[1] FOR i=2 TO NumOfColumns Columns$=WORD$(FndTables$[TableNr],i+2,",") SQL.QUERY Info[i],DB_Ptr,TableName$,Columns$ NEXT i ! Loop through cursors FOR i=1 TO L SQL.NEXT xdone,Info[1],ColDat$ FillRow$="'"+ColDat$+"'" FOR j=2 TO NumOfColumns SQL.NEXT xdone,Info[j],ColDat$ FillRow$=FillRow$+",'"+ColDat$+"'" NEXT j insert$="javascript:fillTable("+FillRow$+")" HTML.LOAD.URL insert$ NEXT i ARRAY.DELETE Info[] DO HTML.GET.DATALINK data$ IF LEFT$(data$,7)="DAT:ins" THEN ! Add column InsCol$=RIGHT$(data$,LEN(data$)-7) SQL.EXEC DB_Ptr,"ALTER TABLE "+TableName$+" ADD "+InsCol$+" TEXT" FndTables$[TableNr]=REPLACE$(FndTables$[TableNr],","+INT$(NumOfColumns),","+INT$(NumOfColumns+1)) NumOfColumns=NumOfColumns+1 FndTables$[TableNr]=FndTables$[TableNr]+","+InsCol$ ENDIF IF LEFT$(data$,7)="DAT:del" THEN ! Delete row DelRow=VAL(RIGHT$(data$,LEN(data$)-7)) SQL.DELETE DB_Ptr,TableName$,"_id='"+INT$(DelRow)+"'" ENDIF IF LEFT$(data$,7)="DAT:mod" THEN in$=RIGHT$(data$,LEN(data$)-8) ModRow=VAL(WORD$(in$,1,",")) FOR i=2 TO NumOfColumns C$=WORD$(FndTables$[TableNr],i+2,",") V$=WORD$(in$,i,",") SQL.UPDATE DB_Ptr,TableName$,C$,V$:"_id='"+INT$(ModRow)+"'" NEXT i ENDIF IF LEFT$(data$,7)="DAT:row" THEN InsRow$=RIGHT$(data$,LEN(data$)-7) ! Get _id and send it back SQL.INSERT DB_Ptr,TableName$,WORD$(FndTables$[TableNr],4,","),"NewlyAdded" SQL.QUERY Info,DB_Ptr,TableName$,"_id",WORD$(FndTables$[TableNr],4,",")+"='NewlyAdded'" SQL.NEXT xdone,Info,Id$ SQL.UPDATE DB_Ptr,TableName$,WORD$(FndTables$[TableNr],4,","),"":WORD$(FndTables$[TableNr],4,",")+"='NewlyAdded'" insert$="javascript:putID("+InsRow$+","+Id$+")" HTML.LOAD.URL insert$ ENDIF UNTIL data$="DAT:fin" | LEFT$(data$,3)="BAK" HTML.CLOSE RETURN MakeTable: HTML$=HTML00$ HTML$=HTML$+"function fillTable(col1" FOR i=2 TO NumOfColumns HTML$=HTML$+",col"+INT$(i) NEXT i HTML$=HTML$+")" HTML$=HTML$+HTML01$ FOR i=1 TO NumOfColumns ! columns to add HTML$=HTML$+"nwRow.insertCell("+INT$(i)+").innerHTML=col"+INT$(i)+";" HTML$=HTML$+"nwRow.cells["+INT$(i)+"].style.backgroundColor='#CCFFFF';" NEXT i HTML$=HTML$+"nwRow.cells[1].style.color='#FF0000';" HTML10$="var numColumns=document.getElementById('fixedHead').rows[0].cells.length;" HTML$=HTML$+HTML10$ HTML$=HTML$+HTML02$ HTML$=HTML$+HTML10$ HTML$=HTML$+HTML03$ HTML$=HTML$+HTML10$ HTML$=HTML$+HTML04$ HTML$=HTML$+HTML10$ HTML$=HTML$+HTML05$ HTML$=HTML$+HTML10$ HTML$=HTML$+HTML06$ ! Index HTML$=HTML$+"row.insertCell(1).innerHTML='"+WORD$(FndTables$[TableNr],3,",")+"';" FOR i=2 TO NumOfColumns ! column headers to create HTML$=HTML$+"row.insertCell("+INT$(i)+").innerHTML='"+WORD$(FndTables$[TableNr],i+2,",")+"';" NEXT i HTML$=HTML$+HTML07$ ! Index HTML$=HTML$+"row.insertCell(1).innerHTML='"+WORD$(FndTables$[TableNr],3,",")+"';" HTML$=HTML$+"row.cells[1].style.backgroundColor='#FF0000';" FOR i=2 TO NumOfColumns ! column headers to create HTML$=HTML$+"row.insertCell("+INT$(i)+").innerHTML='"+WORD$(FndTables$[TableNr],i+2,",")+"';" HTML$=HTML$+"row.cells["+INT$(i)+"].style.backgroundColor='#FF00FF';" NEXT i HTML$=HTML$+HTML08$ HTML$=HTML$+"" HTML$=HTML$+HTML09$ RETURN