SQLite database problems - android

I have veritabani.java and i create database in this class. Then i create object in mainactivity.java. But when i run app. the program dont move database.java so i cant create database how i can solve this problem.
veritabani.java
public class veritabani extends SQLiteOpenHelper {
private static final String VERİTABANİ_ADİ="kayit";
public veritabani(Context c)
{
super(c,VERİTABANİ_ADİ,null,2);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE kayit(ilacadi TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXIST kayit");
onCreate(db);
}
}
mainactivity.java
public class MainActivity extends Activity {
private veritabani v1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
v1=new veritabani(this);
}
v1=new veritabani(this) doesnt work.

Try this code
public class DBAdapter
{
private static final String DATABASE_NAME = "GSDATA.db";
private static final int DATABASE_VERSION = 2;
private static final String CREATE_COVERPHOTO_TABLE = "CREATE TABLE IF NOT EXISTS COVERPHOTO (path Text,date DATE);";
private final Context context;
public String group;
private DatabaseHelper DBHelper;
private static SQLiteDatabase db;
public DBAdapter(Context ctx)
{
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
db.execSQL(CREATE_COVERPHOTO_TABLE);
System.out.println("The Database is Created Here :");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// db.execSQL("DROP TABLE IF EXISTS calSimpleNote");
onCreate(db);
}
} // database helper class complete
// ---opens the database---
public DBAdapter open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
// ---closes the database---
public void close() {
DBHelper.close();
db.close();
}
// and write below line in main activity
DBAdapter adapter = new DBAdapter(MainActivity.this);

I'm not sure what you mean by "the program dont move database.java" but i had issues with databases using SQLiteOpenHelper.
Actually, SQLiteOpenHelper calls " public void onCreate(SQLiteDatabase db)" only once, so if you change your tables between 2 launch, it won't be updated.
To solve this you have to manually delete the database by going into parameters / applications / [your_app] / erase data (or something like this) and try again.

Related

sqlite can not create table

I write SQL code at small android application, always database create successfully, but that is not correct for the table.
public dbSqlHelper dbHelper;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dbHelper = new dbSqlHelper(this);
SQLiteDatabase dataSql = dbHelper.getWritableDatabase();
}
that is the main, and the following code for dbhelper
private static final String DATABASE_NAME = "malekData.db";
private static final int DATABASE_VERSION = 1;
private Context context;
public dbSqlHelper(#Nullable Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate( SQLiteDatabase db) {
try {
db.execSQL("CREATE TABLE malekDB4 ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT);");
} catch (SQLException e) {
Log.e("Malek", "Error when creating table: " + e.getMessage());
}
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
when I go to terminal and check table (.tables), no thing return

Insert data in sqlitedatabase using AndroidStudio

I am trying to insert data in the table MyProduct but every time i run the program it shows "null pointer exception" in db.insertData() in MainActivity and method insertData() in MyHelper class. Can anyone please tell me whats going wrong?? Thank You
MainActivity
public class MainActivity extends Activity {
MyHelper helper;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
helper=new MyHelper(this);
SQLiteDatabase sqLiteDatabase=helper.getWritableDatabase();
// helper.onCreate(sqLiteDatabase);
helper.insertData();
}
}
MyHelper.class
public class MyHelper extends SQLiteOpenHelper {
static final String DATABASE_NAME="MyDatabase";
static final int DATABASE_VERSION=1;
static final String TABLE_NAME="MyCatalog";
static final String UID="ID";
static final String UTITLE="TITLE";
static final String UPRICE="PRICE";
static final String UDESCP="DESCP";
static final String CREATE_TABLE="CREATE TABLE"+TABLE_NAME+"("+UID+"VARCHAR(25) PRIMARY KEY,"+UTITLE+ " VARCHAR(255),"+UPRICE+" VARCHAR(255),"+UDESCP+" VARCHAR(255));";
private static final String DROP_TABLE="DROP TABLE" +TABLE_NAME+"IF EXISTS";
private Context context;
public MyHelper(Context context)
{
super(context,DATABASE_NAME,null,DATABASE_VERSION);
this.context=context;
Message.message(context,"Constructor called");
}
public void onCreate(SQLiteDatabase db)
{
Message.message(context,"on create");
db.execSQL(CREATE_TABLE);
Message.message(context,"table created");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
Message.message(context,"upgrade called");
db.execSQL(DROP_TABLE);
onCreate(db);
}
ContentValues contentValues;
public SQLiteDatabase db;
public void insertData()
{
contentValues.put(UID,"W");
contentValues.put(UTITLE,"w");
contentValues.put(UPRICE,"10");
contentValues.put(UDESCP, "abc");
db.insert(TABLE_NAME, null, contentValues);
}
}
You haven't initialized your db or contentValues objects. At the start of your insertData method, try putting the following:
db = getWriteableDatabase();
contentValues = new ContentValues();

passing context from non-activity

i yet really grasp this whole context thing we found a lot in android programming. so i tried creating a function to drop all my tables, and here's a my partial code:
public class DBAdapter {
private static class DbHelper extends SQLiteOpenHelper {
private boolean databaseCreated = false;
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void deleteTables(){
Log.d("DBAdapter","dlm drop tables pre");
this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_TV+"];");
this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_CAMERA+"];");
this.sqlDatabase.execSQL("DROP TABLE IF EXISTS ["+TABLE_GPS+"];");
}
}
}
and the part where i'm going to call the function deleteTables
public class UpdateDatabase {
public void updateTable(String table,JSONObject jsonObject){
DBAdapter db = new DBAdapter(this);
db.deleteTables();
}
}
but of course it will return an error, since DBAdapter expects a context. public class UpdateDatabase is not an activity class. Calling DbAdapter db = new DBAdapter(this) from activity class will work just find. So how do I find any fix for this problem?
thanks
You can add a constructor to UpdateDatabase that takes a Context and stores it so that it is available to be used by updateTable. Something like this:
public class UpdateDatabase {
private final Context mContext;
public UpdateDatabase(Context context){
mContext = context;
}
public void updateTable(String table,JSONObject jsonObject){
DBAdapter db = new DBAdapter(mContext);
db.deleteTables();
}
}
Now, whenever you do new UpdateDatabase() you will need to do new UpdateDatabase(..context..) instead. If you are doing this from an Activity, then you can do new UpdateDatabase(this).
hi look this code..
public class DbManager
{
// the Activity or Application that is creating an object from this class.
Context context;
CustomSQLiteOpenHelper helper;
// a reference to the database used by this application/object
protected SQLiteDatabase db;
private static DbManager INSTANCE;
// These constants are specific to the database.
protected final String DB_NAME = "yourDB";
protected final int DB_VERSION = 1;
public DbManager(Context context)
{
this.context = context;
// create or open the database
helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
public static DbManager getInstance(Context context){
if(INSTANCE == null)INSTANCE = new DbManager(context);
return INSTANCE;
}
public void db_Close()
{
if(helper!=null){
helper.close();
}
this.db.close();
}
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db)
{
// This string is used to create the database.
// execute the query string to the database.
//db.execSQL(newTableQueryString);
Log.i("DataBaseManager", "Create Table");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}
}
// Inherit the DbManager Class
public class DataCollection extends DbManager {
public DataCollection(Context context){
super(context);
}
public void deleteTable(String TABLE_NAME){
try {db.execSQL("DROP TABLE "+TABLE_NAME);}//.delete(TABLE_NAME, null, null);}
catch (Exception e){
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}

Android SQLite and Method for it

How to declare database in android? How we make a database in android and method?
Basically you extend a class with SQLiteOpenHelper and implement onCreate and onUpgrade.
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "db_table";
public DatabaseHelper2(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE coordinate (field1 CHAR NOT NULL ,field2 INTEGER NOT NULL ,field3 INTEGER NOT NULL);");
db.execSQL("INSERT INTO spotit VALUES( test ,10,10);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
Then initiate the class in your main activity.
DatabaseHelper db = new DatabaseHelper(this);
db.getReadableDatabase();
db.close();
onCreate will be invoked when you call db.getReadableDatabase()
all you need is here:
http://developer.android.com/guide/topics/data/data-storage.html#db

GetReadableDatabase error android

I created a DBTest class SQLiteOpenHelper
Then I called this from my main UI.
It crashes when I hit the DB.GetReadableDatabase () and the log is no help, just says null pointer, but I don't know where to look.
Everything works if I use
SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
Same database
Here is the Class for the helper and below the error:
public class DBTest extends SQLiteOpenHelper {
private static String DB_NAME = "DB";
private SQLiteDatabase myDataBase;
private final Context myContext;
public DBTest(Context context) {
super(context, DB_NAME, null, 1);
this.myContext = context;
}
#Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
Below is where the error happens:
public class Main extends Activity
{
DataBaseHelper db = new DataBaseHelper(null);
static SQLiteDatabase Db;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SQLiteDatabase Db;
DBTest db1 = new DBTest(null);
Db = db1.getReadableDatabase(); <<< blow up here
}
}
DBTest db1 = new DBTest(this.getApplicationContext());
Context can't be null

Categories

Resources