SQLite extension

extension

#1

PedrozaSQLite

Hello! Here I present to you my second extension ever created! It is an extension to access the local SQLite database!

Background

I know there is already a few extensions with this functionality, but here are the reasons why I made it:

  • First of all, thanks to AppInventor and all its distributions I took my first steps on how to code. Later, I thought about creating extensions and I found that I really like Java and the whole process of thinking and trying to run it correctly. So I took my chances and made my first extension that I published it in Thunkable (Polyline Tools).
  • I knew that I wanted more, and decided to make one of the extensions I always wanted to have but couldn’t afford it.
  • My goal is to let other people like me use this extension, but always remembering the true meaning of AppInventor that is learning how to code. So if you feel like you can take the next step, I encourage you to try it. It’s really fun!
  • Also, by letting other people sponsor this extension I would have the opportunity to earn enough money to create my Developer Account.

So this is it! I hope you find this useful and if you find how to make it better, please let me know so I can learn from it!


For reference on how to write SQLite statements, I recommend for a good quick start TutorialPoint.com and SoloLearn’s SQL Fundamentals.


For other options availables:

  • Taifun’s SQLite Extension - Link (since Aug 11th, 2016).
  • Andrés Cotes’ SQLite Extension - Link (since Oct 2nd, 2017).
  • Juan Ruvalcaba’s SQLite Extension - Link (since September 8th, 2017).

Description

Tool developed by Carlos Pedroza to access the application’s SQlite database based on SQLite Android Developer’s Reference.


Features

  • SQL Statements executes asynchronously, very useful in large databases.
  • Good practices to avoid as possible SQL Injections.
  • Opportunity to use rawQuery and write SQL statements.
  • Compile a statement to reuse it later (on next release).
  • Query result in a list for easy use.

DISADVANTAGES

  • Not able to export and import databases as a file .sqlite (I don’t know how to do it, yet).

Events

  • AfterExecution: Event handler after the SQL statement is executed, returns whether the execution was succesfully executed.
    515ce0724036f4b7220acf171ec4088b2f1747c2

  • AfterQuery: Event handler after the ExecuteRawQuery or Query is executed and returns a list with the selected data and number of records.
    image

  • ErrorOcurred: Event handler when an error ocurred, returns a string with a message from the error.
    image

Methods

  • Query: Executes pre-compiled QUERY statement with specified parameters. Parameters: 1) String table: Name of the table. 2) YailList columns: List of which columns to return, passing an empty list will return all columns. 3) String selection: Filter declaring which rows to return, formatted as an SQL WHERE clause, passing an empty string will return all rows. 4) YailList selectionArgs: List with the arguments that will replace onto ‘?’ in the selection filter. 5) String groupBy: A filter declaring how to group rows, formatted as an SQL GROUP BY clause (excluding the GROUP BY itself), passing an empty string will cause the row to not be grouped. 6) String having: A filter declare which row groups to include if row grouping is being used, passing an empty string will cause all row groups to be included. 7) String orderBy: How to order the rows, formatted as an SQL ORDER BY clause (excluding the ORDER BY itself), passing an empty string will use the default sort order (unordered). 8) String limit: Limits the number of rows returned by the query, formatted as LIMIT clause, passing an empty string denotes no LIMIT clause. Return: The result query is available in the AfterQuery event handler.
    image

  • Insert: Executes pre-compiled INSERT statement with specified parameters. Parameters: 1) String table: Name of the table. 2) YailList columns: List with the columns that will contain the data to be inserted in the database. 3) YailList values: List with the data to be inserted in the database. Returns the row ID of the newly inserted row, or -1 if an error occurred.
    image

  • Replace: Executes pre-compiled REPLACE OR INSERT INTO statement with specified parameters. Parameters: 1) String table: Name of the table. 2) YailList columns: List with the columns that will contain the data to be replaced in the database. 3) YailList values List with the data to be replaced in the database. Returns the row ID of the newly replaced row, or -1 if an error occurred.
    image

  • Update: "Executes pre-compiled UPDATE statement with specified parameters. Parameters: 1) String table: Name of the table. 2) YailList columns List with the columns that will contain the data to be inserted in the database. 3) YailList values List with the data to be inserted in the database. 4) String whereClause: optional WHERE clause to apply when updating, leave an empty string to update all rows. Include ?s, which will be updated by the values from whereArgs. 5) YailList whereArgs: List with the columns that will contain the data to be updated in the database. Returns the row ID of the newly inserted row, or -1 if an error occurred.
    image

  • Delete: Executes pre-compiled DELETE statement with specified parameters. Parameters: 1) String table: Name of the table. 2) String whereClause: Optional WHERE clause to apply when deleting (Example: ‘ID = ?’), pasing an empty a string will delete all rows. 3) List whereArgs: List with arguments for the WHERE clause. These arguments will be replaced by ‘?’ in the whereClause. Returns the number of rows affected if a whereClause is passed in, 0 otherwise.
    image

  • SingleSQL: Execute a Single SQL Statement asynchronously and returns whether the transaction was succesful in the AfterExecution Event Handler. Use it when returned data isn’t needed. Parameters: String sql.
    image

  • MultipleSQL: Execute Multiple SQL Statement asynchronously and returns whether the transaction was succesful in the AfterExecution Event Handler. Use it when returned data isn’t needed. Parameters: List of SQL statements.
    image

  • RawQuery: Executes the provided rawQuery Statement asynchronously. Returns a YailList with the selected data and number of records in the AfterQuery Event. Parameters: 1)String SQL statement. 2) List selectionArgs: List with the arguments that will replace ‘?’ in where clause in the query, to prevent SQL injections.
    image

  • GetPath: Returns the path to the database.
    image

  • ClearDatabase: Clears the database to version 1. Use only while developing, this shouldn’t be used on production.
    image

Properties

  • ReturnHeader:
    Returns whether the header row should be returned in the result of a Select statement.
    image
    Specifies whether the header row should be returned in the result of a Select statement.
    image
  • SupressToast:
    Returns whether Success Toast should be suppressed.
    image
    Specifies whether Success Toast should be suppressed.
    image

Best Practices

In order to avoid SQL Injections and use pre-compiled statements, it is a good practice to use this blocks to SELECT, INSERT, UPDATE, REPLACE and DELETE.
image

On the other hand, these block are useful for executing a single SQL statement that is NOT a SELECT or any other SQL statement that returns data (INSERT, UPDATE, REPLACE or DELETE). It has no means to return any data (such as the number of affected rows). Statements like CREATE or DROP table / trigger / view / index / virtual table, ALTER TABLE, REINDEX, RELEASE, SAVEPOINT and PRAGMA are very good options to use these block for.
image

When using rawQuery (using it a lot will cause performance issues because it compiles it over and over again) use the selectionArgs to provide the arguments that will replace ‘?’ in the where clause to prevent SQL injections. For example: SELECT * FROM table_name WHERE ID=?, the arguments in selectionArgs will replace all ?.
image


Price

This extension has been sponsored by @Peter and @Diego_Marino! Thank you so much!

If you find this useful, you still can donate!
Donation Link: http://paypal.me/DrCarlosPedroza


Downloads

.AIX: com.pedroza.PedrozaSQLite.aix (23.4 KB)
.JAVA Available at: Github
Test .APK: PedrozaSQLite.apk (1.6 MB)
Test .AIA: PedrozaSQLite.aia (32.9 KB)


Changelog

  • October 9, 2017 - Initial release.

UPCOMING:

  • More functions like CompileStatements in order to reuse precompiled statements.
  • Video tutorial :smiley:
  • Video tutorial on how to write SQLite statements

Best Regards from :venezuela:,
Carlos Pedroza


My first app in AppyBuilder
Not Always Firebase
#2

Very cool extension, @CarlosPedroza27… Thank you for your contribution…:relaxed:


#3

hi cool
extension and i want to tes it wih this quere
SELECT Column1 FROM Marker WHERE ABS(latitude - 43.21975) < 0.001 AND ABS(longitude - 23.54455) < 0.001

but in call quere block
what i mus put from code above in section, sectionargs, groupBy and having


#4

sqlite is a built in component in Appybuilder, so there is no need to use an extension…
Taifun


#5

Stop posting your question all over the community. I guess you want an answer but this is not helping.


#6

The project .aia , it does not work you can upload it again thank you


#7

can I do this?
SELECT
SUM(milliseconds)
FROM
tracks;
If it is possible pls send me a screen capture of blocs or aia file
Thanks