Android Tutorial SQL tutorial

Status
Not open for further replies.

scrat

Active Member
Licensed User
Longtime User
Hi,

How open a sqlite database in readonly mode ?

B4a expose OPEN_READWRITE and CREATE_IF_NECESSARY but not OPEN_READONLY

Thanks
 

scrat

Active Member
Licensed User
Longtime User
It's not for reading from assets folder

I try with this in line java

B4X:
#If Java
import android.database.sqlite.SQLiteDatabase;

public static  SQLiteDatabase openDBro (String P){
   return SQLiteDatabase.openDatabase(P,null,1);
}

#end if

but how to cast android.database.sqlite.SQLiteDatabase to anywheresoftware.b4a.sql.SQL
 

scrat

Active Member
Licensed User
Longtime User
thank you Erel. problem solved by adding a function intitializeReadOnly to your library
 

scrat

Active Member
Licensed User
Longtime User
Ok,
I did not know that it is allowed to share a B4A modified library.

Just 2 lines added to original Sql.jar
B4X:
public void InitializeReadOnly(String Dir, String FileName)
{
   this.db =   SQLiteDatabase.openDatabase(new File(Dir, FileName).toString(), null, 1 | 0x10);
}

Now it's possible to open a Sqllite database in readonly mode with InitializeReadOnly(Dir,FileName)
 

Attachments

  • Sql_RO.zip
    7.3 KB · Views: 502

Mahares

Expert
Licensed User
Longtime User
Now it's possible to open a Sqllite database in readonly mode with InitializeReadOnly(Dir,FileName)
This code returns an error:
B4X:
Sub Process_Globals
    Dim sql As SQLperso
End Sub
If sql.IsInitialized =False Then
    sql.InitializeReadOnly(File.DirRootExternal & "/test","test.db")
End If
B4A version: 5.80
Parsing code. (0.00s)
Compiling code. (0.02s)
Compiling layouts code. (0.00s)
Generating R file. (0.10s)
Compiling generated Java code. Error
B4A line: 36
sql.InitializeReadOnly(File.DirRootExternal & \
javac 1.7.0_80
src\b4a\example\main.java:369: error: cannot find symbol
Debug.DebugWarningEngine.CheckInitialize(_sql);_sql.InitializeReadOnly(anywheresoftware.b4a.keywords.Common.File.getDirRootExternal()+"/test","test.db"); ^
symbol: method InitializeReadOnly(String,String)
location: variable _sql of type SQL

EDIT 3/27/2016 10 PM GMT: Found the problem: Only works if the native SQL library by Erel is unchecked. Therefore, only SQLperso must be checked; otherwise error.
 
Last edited:

Mikonios

Active Member
Licensed User
Longtime User
When "BeginTransaction / EndTransaction" is used as the DB is blocked, record-level, table-level or database-level ??
 
Last edited:

mtechteam

Member
Licensed User
Longtime User
Near the top of this thread, this statement is made:
Usually you will want to declare the SQL object as a process global object. This way it will be kept alive when the activity is recreated.

Rather than in a activity, I have done so in a module. However, when I get a error (a catch in a try structure caused by an SQL error), it seems that if I am in a different activity that where I initialized this object, the MsgBox call (displaying the error) hangs. Is this something I should not be doing, or are there other things at play here.
 

mangojack

Well-Known Member
Licensed User
Longtime User
Near the top of this thread, this statement is made:
Usually you will want to declare the SQL object as a process global object.
Since that was written , the Starter service has been introduced. See Here .... Ideally this is where you should now declare / initialize SQL object.
Secondly .. if the message box is used for debug purposes only .. avoid and use Log(...) instead.

If you have further problems it might be wise to start a new thread in questions , with some code or uploading a demo project.
 

Ricardo Gonzalez Gaete

Member
Licensed User
Longtime User
Existen campos auto numéricos en SqlLite, y como se crean por código, por favor...
sql1.execnonquery("ALTER TABLE VTATERRENO ADD COLUMNS ID INTEGER PRIMARY KEY AUTOINCREMENT")
 

klaus

Expert
Licensed User
Longtime User
As LucaMs already said, you cannot add a new coumn with the INTEGER PRIMARY KEY AUTOINCREMENT definition, see here.
Instead of an INTEGER PRIMARY KEY AUTOINCREMENT column you can use the SQLite internal column rowid which does the same.
You may have a look at chapters 5 SQLite Database and more specific 5.1.3 INTEGER PRIMARY KEY rowid in the B4A User's Guide.
 

luciano deri

Active Member
Licensed User
Longtime User
Hello everyone. I have an java error when convert blob in img.
B4X:
sub WriteBlob
InputStream1 As InputStream
File.Exists(Main.PthIO,"logo.jpg") Then
    InputStream1 = File.OpenInput(Main.PthIO,"logo.jpg")
    Dim OutputStream1 As OutputStream
    OutputStream1.InitializeToBytesArray(1000)
    File.Copy2(InputStream1, OutputStream1)
    buffer = OutputStream1.ToBytesArray
End If
    If buffer.Length > 0 Then
        Main.dbSql.ExecNonQuery("UPDATE aziende set logo = '" & Array As Object(buffer) & "' WHERE  codice = '04'")
    End If

end sub
This is the record on db after write
B4X:
codice         ragosoc                            logo
04    AZIENDA VINICOLA    [Ljava.lang.Object;@21a715da
B4X:
sub ReadBlob
Dim img As Bitmap
       Dim Buffer() As Byte 'declare an empty byte array
        Buffer = DbCurAzie.Getblob("logo")
        Dim i As Long
        Try
            i = Buffer.Length
        Catch
            i = 0
        End Try
        If i > 0 Then
            Dim InputStream1 As InputStream
            InputStream1.InitializeFromBytesArray(Buffer, 0, Buffer.Length)
            Try
             img.Initialize2(InputStream1) ' in this point exit with the error
            Catch
                Log(LastException)
            End Try
            InputStream1.Close
        End If

end sub
This is the error
java.lang.RuntimeException: Error loading bitmap.
at anywheresoftware.b4a.objects.drawable.CanvasWrapper$BitmapWrapper.Initialize2(CanvasWrapper.java:521)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at anywheresoftware.b4a.shell.Shell.runVoidMethod(Shell.java:753)
......
 
Last edited:

luciano deri

Active Member
Licensed User
Longtime User
if i do this query command
B4X:
    Main.dbSql.ExecNonQuery("UPDATE aziende set logo = " & Array As Object(buffer) & " WHERE  codice = '04'")
brake with this error

android.database.sqlite.SQLiteException: unrecognized token: "[Ljava.lang.Object;@26a95a5 WHERE codice = '04'" (code 1): , while compiling: UPDATE aziende set logo = [Ljava.lang.Object;@26a95a5 WHERE codice = '04'

i don't understund how do update with ExecNonQuery2
B4X:
dbSql.ExecNonQuery2("UPDATE aziende set('logo', ?) WHERE codice = '04'", Array As Object(buffer))
???
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…