You are on page 1of 11

Puppycode Blog

Dummy for programmers

Module 5 B4A Sample Login Program Using Database SQLite


Browser
B4A (Basic4Android) SAMPLE PROGRAM

STEP 1. Open your b4a

Step 2. Save your program as Sample

Step 3. Open SQLite Browser and create data base

Step 4. Create database with file name dbsample.db

Step 5. Create table name accounts with 3 Fields [id], [username] and [password] then press OK

Insert data in New Record


username : adminDB2

password: adminDB2

username : adminDB

password: adminDB

Step 6. Now create your Design by clicking Designer -> Open Designer in the Menu bar

Step 7. Now add 1 Button , 2 Labels and 2 EditText components

Step 8. Change the component name in the properties


Button= btnlogin
EditText1= txtusername
EditText2= txtpassword

And for the Label change the TEXT in the properties


Label1 = Username
Label2= Password
Btnlogin= Login

Step 9. Every component should be initialize.

Step 10. Go back designer and right click the Button named btnlogin and generate Click

Add this codes to the btnlogin_click


Dim cursor1 As Cursor
If txtusername.Text = And txtpassword.Text= Then
Msgbox(Please fill up all the fields,invalid)
Else
cursor1=SQL1.ExecQuery(Select * from accounts where username='& txtusername.Text & and password ='& txtpassword.Text &)
If cursor1.RowCount<>0 Then
Msgbox(Login Successful,)
Else
Msgbox(Please check your username or your password,wrong input)
End If
End If

Step 11. Save your Design as loginlayout

Step 12. Now we are going to create a simple program using DATABASE
My database name is datasample.db
these are the codes you are going to use to connect your database to your program

If File.Exists(File.DirInternal,datasample.db) = False Then


File.Copy(File.DirAssets,datasample.db,File.DirInternal,datasample.db)
End If
If SQL1.IsInitialized = False Then
SQL1.Initialize(File.DirInternal, datasample.db, False)
End If

and check the SQL in the library Manager and the


Dim SQL1 As SQL
Dim cursor1 As Cursor

Configure the File manager ,add your database to your file manager

Step 13. If you want to test the program, download the b4a application in your android phone

Make sure that your computer/laptop and your android phone connection belongs to the same network

My IP address in my computer is 192.168.1.116 and also in my android phone

Step 14. Configure the connectivity in the b4a in your computer


Connect in to your IP

Check the connection in to your android phone


If your status is connected, both devices are connected.
If is not please check your IP in your computer or your network connection or Click START-Wireless

Step 15. Run your program

Step 16. Check your mobile phone and install the b4A example

Click the login button without any input in the username and password the output should be

Now input admin in the username and admin123 in password the output should be

Now input adminDB in the username and adminDB in the password


or input adminDB2 in the username and adminDB2 in the password the output should be

NEXT TUTORIAL: CRUD


NEXT

Proudly powered by WordPress | Theme: Oblique by Themeisle.

You might also like