Download the free trial version
Basic4android Video
Features
Tutorials and manuals
Showcase
Screenshots

Go Back   Android Development Forum - Basic4android > Basic4android > Basic4android Getting started & Tutorials
Documentation Wiki Register Members List B4P Search Today's Posts Mark Forums Read

Basic4android Getting started & Tutorials Android development starts here. Please do not post questions in this sub-forum.

DBUtils - Android databases are now simple!

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-30-2011, 11:22 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default DBUtils - Android databases are now simple!

The DBUtils code module is designed to help you integrate SQLite databases in your program.
It contains a set of methods for common SQL tasks. Issues like escaping strings and identifiers are handled automatically.
The code module is included in the attached example project.
As of version 1.07, the following methods are included:

- CopyDBFromAssets: Copies a database file stored in the APK package (added in the Files tab) to a writable location. Only copies if the file doesn't yet exist.

- CreateTable: Creates a SQL table with the given fields and types. Also sets the primary key (optionally).

- DropTable: Deletes a table.

- InsertMaps: Efficiently inserts records to a table. The data is passed as a List that contains maps as items. Each map holds the fields and their values.
Example:
Code:
    Dim ListOfMaps As List
ListOfMaps.Initialize
For i = 1 To 40
    
Dim m As Map
    m.Initialize
    m.Put(
"Id", Id)
    m.Put(
"First Name""John")
    m.Put(
"Last Name""Smith" & i)
    ListOfMaps.Add(m)
Next
DBUtils.InsertMaps(
SQL"Students", ListOfMaps)
- UpdateRecord: Updates an existing record. Accepts the field name with the new value and a map of the 'where' fields.
Example:
Code:
    Dim WhereFields As Map
WhereFields.Initialize
WhereFields.Put(
"id""1234")
WhereFields.Put(
"test""test #5")
DBUtils.UpdateRecord(
SQL"Grades""Grade"100, WhereFields)
- ExecuteMemoryTable: Executes a SELECT query and reads all the records into a list. Each item in the list is an array of strings that represents a record in the result set.

- ExecuteMap: Executes a SELECT query and returns the first record stored in a Map object. The columns names are the keys and the values are the map values.

- ExecuteSpinner: Executes a SELECT query and fills a Spinner view (ComboBox) with the values of the first column.

- ExecuteListView: Executes a SELECT query and fills the ListView with the values of the first column and optionally of the second column.

- ExecuteJSON: Executes a SELECT query and returns a Map which you can pass to JSONGenerator to generate JSON text.
Example:
Code:
    Dim gen As JSONGenerator 'Requires a reference to the JSON library.
gen.Initialize(DBUtils.ExecuteJSON(SQL"SELECT Id, [Last Name], Birthday FROM Students"Null, _
    
0Array As String(DBUtils.DB_TEXT, DBUtils.DB_TEXT, DBUtils.DB_INTEGER)))
Dim JSONString As String
JSONString = gen.ToPrettyString(
4)
Msgbox(JSONString, "")
See the attached example and the DBUtils code module for more information about each method.

- ExecuteHtml: Creates a nice looking html table out of the results. You can show the results using WebView. This method is very useful both for development, as it allows you to see the data and also for reports.
You can change the table style by changing the CSS variable.
New: The last parameter defines whether the values will be clickable or not.
If the values are clickable then you should catch WebView_OverrideUrl event to find the clicked cell.


- GetDBVersion / SetDBVersion: Gets or sets the database version. The value is stored in a separate table named DBVersion. This is useful to manage updates of existing databases.
This feature was implemented by corwin42. Thank you!

About the example. In this example we are creating two tables: Students and Grades. The students table lists all the students.
The grades table lists the grade of each student in each test.

Uncomment ExportToJSON or ShowTableInWebView calls to see those features.





To hide the table, comment the call to ShowTableInWebView in Activity_Create.
Attached Files
File Type: zip DBUtils.zip (12.0 KB, 1688 views)
Reply With Quote
  #2 (permalink)  
Old 03-30-2011, 01:21 PM
Basic4ppc Expert
 
Join Date: Jan 2011
Location: Swindon UK
Posts: 566
Default

This will be extremely useful, Thanks Erel

Steve
Reply With Quote
  #3 (permalink)  
Old 03-31-2011, 07:46 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

V1.01 posted with ExecuteJSON which exports the result as JSON text.
Reply With Quote
  #4 (permalink)  
Old 03-31-2011, 10:01 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

V1.05 posted with ExecuteHtml. This one is really useful. It allows you to display tables easily. See the above screenshot.
Reply With Quote
  #5 (permalink)  
Old 03-31-2011, 04:22 PM
derez's Avatar
Basic4ppc Expert
 
Join Date: May 2007
Posts: 978
Awards Showcase
Beta Tester Competition Winner 
Total Awards: 2
Default

John smith1 is three years old, no wonder he failed on exam #6.
John smith8 was born 1916 and is still a student !
__________________
David Erez
Ramat Hasharon, Israel
Reply With Quote
  #6 (permalink)  
Old 03-31-2011, 04:25 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Quote:
John smith8 was born 1916 and is still a student !
I admire his determination.
Reply With Quote
  #7 (permalink)  
Old 03-31-2011, 08:59 PM
Knows the basics
 
Join Date: Jan 2011
Posts: 82
Default

An amazing addition to an amazing product.
Reply With Quote
  #8 (permalink)  
Old 04-01-2011, 01:07 PM
JMB JMB is offline
Knows the basics
 
Join Date: Jan 2011
Location: Shetland Islands, UK
Posts: 67
Default

I've ripped out about 30% of my rather inflexible code and replaced it with calls to DBUtils!

Absolutely superb. Thanks again. What a superb product this is!
Reply With Quote
  #9 (permalink)  
Old 04-10-2011, 06:15 AM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Version 1.06 is attached to the first post. It fixes a serious bug discovered by JMB.

You can apply the fix by changing line 141 to:
Code:
Dim values(c.ColumnCount) As String
Instead of:
Code:
Dim values(c.RowsCount) As String
Reply With Quote
  #10 (permalink)  
Old 05-12-2011, 12:38 PM
Erel's Avatar
Administrator
 
Join Date: Apr 2007
Posts: 15,689
Awards Showcase
Basic4ppc Founder 
Total Awards: 1
Default

Using the new WebView_OverrideUrl event, the html table can now be made of clickable links instead of just text. This allows you to catch a cell selection event with the following code (included in the example):
Code:
Sub WebView1_OverrideUrl (Url As StringAs Boolean
    
'parse the row and column numbers from the URL
    Dim values() As String
    values = 
Regex.Split("[.]", URL.SubString(7))
    
Dim col, row As Int
    col = values(
0)
    row = values(
1)
    
ToastMessageShow("User pressed on column: " & col & " and row: " & row, False)
    
Return True 'Don't try to navigate to this URL
End Sub
The current CSS style makes the links look like regular text.

If you need the actual value you can first call ExecuteMemoryTable and then when the user clicks on a cell you can fetch the values from the memory table (list of arrays).
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Android SlidingPanels - Simple way to create sliding layouts Erel Basic4android Getting started & Tutorials 76 05-09-2012 11:44 PM
Anyone can help me design one simple Android Apps with Basic4android LouieChan Basic4android Updates and Questions 5 04-16-2012 12:32 AM
Syncing Databases JasonPoS Forum Discussion 0 10-12-2010 10:15 PM
Attaching databases mozaharul Questions (Windows Mobile) 1 06-17-2010 06:42 PM
Synchronising databases BjornF Questions (Windows Mobile) 2 10-18-2007 07:26 AM


All times are GMT. The time now is 10:36 AM.


Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0