Hello everyone!
Here is an example code for creating a database in SQLite and connecting it with android application.
SQLite is simple database developed to run on small devices with limited storage and processing capability like mobile phones.
Follow these steps:
1. create an android application with any name for example, SqliteDatabaseTest
2. create a new class DatabaseHelper.
3. Write the following code in MyDatabaseHelper.java class.
4. Add a TextView in the layout xml file of your application
5. Now include the following code, in the MainActivity.java class:
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends Activity {
TextView txtData;
StringBuilder str = new StringBuilder(“”);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtData = (TextView)findViewById(R.id.txtData);
MyDatabaseHelper helper = new MyDatabaseHelper(this);
helper.addStudent(“A”,”123″);
helper.addStudent(“B”,”456″);
helper.addStudent(“C”,”789″);
Cursor cursor = helper.getStudent();
cursor.moveToFirst();
while(!cursor.isAfterLast()){
str.append(cursor.getString(1)+” “+cursor.getString(2)+”\n”);
cursor.moveToNext();
}
txtData.setText(str);
}
}
6. Run application on the AVD or your device. It would display all the inserted records in the TextView.
That’s it!
Try to run this code and customize it to fit your needs.
Thank You!
Code On! 😀