SQLite database in android is not creating i have used method getwritabledatabase(); in main class... but the only log appears "Constructor is called" and then application is closed. Here i have code for table extends SQLiteOpenHelper class
enter code here
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.widget.Toast;
import java.sql.SQLDataException;
import java.sql.SQLException;
public class Table extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Hammas's Database";
private static final String TABLE_NAME = "Hammas's Table";
private static final int DATABASE_VERSION = 1;
private static final String ID = "_id";
private static final String COLNAME = "Names";
private static final String CREATE_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+COLNAME+" VARCHAR(255));";
private static final String DROP_TABLE = "DROP TABLE IF EXIST "+TABLE_NAME+"";
private static final String TAG = "Hammas";
private Context context;
public Table(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
Message.message(context,"Constructor is called");
Log.i(TAG,"CONSTRUCTOR IS CALLED");
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
Message.message(context, "onCreate is called");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
Message.message(context,"onUpgrade is called");
}
} Message class is just for toast. Here is mainClass
enter code here
public class MainActivity extends ActionBarActivity {
Table table;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_view);
table = new Table(this);
SQLiteDatabase sql= table.getReadableDatabase();
}
Aucun commentaire:
Enregistrer un commentaire