1
 -------This is my Database activity here i m creating database ----------     

     public void Database_Create(){

    try{
        db = openOrCreateDatabase(Environment.getExternalStorageDirectory()+"/TestExample/Test",MODE_PRIVATE,null);

    }catch (Exception e){

        e.printStackTrace();
    }

}

------ This is my MainActivity ------------------

    try{

        Database dbs = new Database();
        dbs.Database_Create();
        Toast.makeText(dbs, "Data Ok", Toast.LENGTH_SHORT).show();


    }   catch (Exception e){
        e.printStackTrace();
    }

I m creating database in Database Activity and i want to call Database_create() method in MainActivity how do i call ?? i m getting an error db object is null , permission already i have given in manifest.xml file

1

3 Answers 3

0

for first look at this doc,

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
public class DBTest extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);

  SQLiteDatabase myDB= null;
  String TableName = "myTable";

  String Data="";

  /* Create a Database. */
  try {
   myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);

   /* Create a Table in the Database. */
   myDB.execSQL("CREATE TABLE IF NOT EXISTS "
     + TableName
     + " (Field1 VARCHAR, Field2 INT(3));");

   /* Insert data to a Table*/
   myDB.execSQL("INSERT INTO "
     + TableName
     + " (Field1, Field2)"
     + " VALUES ('Saranga', 22);");

   /*retrieve data from database */
   Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);

   int Column1 = c.getColumnIndex("Field1");
   int Column2 = c.getColumnIndex("Field2");

   // Check if our result was valid.
   c.moveToFirst();
   if (c != null) {
    // Loop through all Results
    do {
     String Name = c.getString(Column1);
     int Age = c.getInt(Column2);
     Data =Data +Name+"/"+Age+"\n";
    }while(c.moveToNext());
   }
   TextView tv = new TextView(this);
   tv.setText(Data);
   setContentView(tv);
  }
  catch(Exception e) {
   Log.e("Error", "Error", e);
  } finally {
   if (myDB != null)
    myDB.close();
  }
 }
}
Sign up to request clarification or add additional context in comments.

Comments

0

You can create your database like so:

private SQLiteDatabase db;

private SQLiteDatabase db = openOrCreateDatabase(Environment.getExternalStorageDirectory()+"/TestExample/Test.db", Context.MODE_PRIVATE, null);

Comments

0

This is the complete example for create SQLite database in android sqlite. We need to extend an class with SQLiteOpenHelper. and need to create a constructor to pass parameters from the activity.

public class FlightsDatabase extends SQLiteOpenHelper {
    private Context context;
    public FlightsDatabase(Context context) {
        super(context,"My_Datas",null,1);
        this.context=context;
    }

Then we need to declare the Database name and this have 2-3 options for create database and upgrade the database version when you want some changes in database after publish an app. Then we will create the table as:

@Override
    public void onCreate(SQLiteDatabase db) {

        db.execSQL("CREATE TABLE Flight (id INTEGER PRIMARY KEY AUTOINCREMENT,sr TEXT,today_date TEXT,imei TEXT,too TEXT,fromm TEXT,dept_date TEXT,status1 TEXT,file_url TEXT,plane_pic TEXT,plane_name TEXT,idd TEXT)");
    }

then you can apply operation for searching , checking data and delete operation over a database.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.