Android SQLiteOpenHelper : SOPL from onCreate() method not shown - android

im trying to do a system.out.println("something") inside onCreate() for SQLiteOpenHelper. but i didn't get anything. i even made sure to call getReadableDatabase();
anyone knows why?
this is my sqlite class
public class ChatOpenHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 2;
public static final String DATABASE_NAME = "FeedReader.db";
private static final String CHAT_TABLE_NAME = "Bolster_ME_YOU";
private static final String CHAT_TABLE_CREATE =
"CREATE TABLE IF NOT EXISTS " + CHAT_TABLE_NAME + " (" +
" test VARCHAR(255) );";
private static final String CHAT_TABLE_DELETE =
"DROP TABLE IF NOT EXISTS " + CHAT_TABLE_NAME + ");";
public ChatOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
System.out.println("hello from sqlite");
System.out.println(this.getDatabaseName());
db.execSQL(CHAT_TABLE_DELETE);
db.execSQL(CHAT_TABLE_CREATE);
Log.v("smartdbhelper", "after creation");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
this.onCreate(db);
}
}
//this is the activity class
public class LoginActivity extends ActionBarActivity {
private Button signin;
private Button signup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
signin = (Button)findViewById(R.id.login_signin);
signup = (Button)findViewById(R.id.login_signup);
ChatOpenHelper open = new ChatOpenHelper(this);
SQLiteDatabase sql = open.getReadableDatabase();
sql.close();
open.close();
}
}

SQLiteOpenHelper.onCreate() is only called when the database is created for the first time.
You'd better use Log.d("label", "value") rather than System.out.print().

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

application has stopped after running the application

I am trying to make loging page but this code not run properly
application afer run tym says unfortunately has stopped i don't no why .i am new in android any one plzz help .i am so frustrate
database file!!
public class RegistrationDetailsDataBaseFile extends SQLiteOpenHelper {
public static final String DATABASE_NAME="registrationDataBase.db";
public static final String TABLE_NAME="registrationTable";
public static final String NAME="name";
public static final String PASSWORD="password";
public static final String PHONE="phone";
public static final String ADDRESS="address";
private static final String QUERY="CREATE TABLE "+TABLE_NAME+"(id integer primary key autoincrement"+NAME+"not null"+PASSWORD+"not null"+PHONE+"not null"+ADDRESS+"not null)";
RegistrationDetailsDataBaseFile(Context context){
super(context,DATABASE_NAME,null,1);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(QUERY);
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists"+TABLE_NAME);
onCreate(db);
}
public void setName(String nam){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(NAME,nam);
db.insert(TABLE_NAME,null,contentValues);
db.close();
}
}
send data from this file file
public class MainActivity extends AppCompatActivity {
EditText mEditTextName,mEditTextPassword,mEditTextPhone,mEditTextAddress;
Button mButtonRegistration;
String getName,getPassword,getPhone,getAddress;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mEditTextName=(EditText)findViewById(R.id.name);
mEditTextPassword=(EditText)findViewById(R.id.password);
mEditTextPhone=(EditText)findViewById(R.id.phone);
mEditTextAddress=(EditText)findViewById(R.id.address);
mButtonRegistration=(Button)findViewById(R.id.register);
mButtonRegistration.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getName=mEditTextName.getText().toString();
getPassword=mEditTextPassword.getText().toString();
getPhone=mEditTextPhone.getText().toString();
getAddress=mEditTextAddress.getText().toString();
RegistrationDetailsDataBaseFile objOfRegistrationDetailsDataBaseFile=new RegistrationDetailsDataBaseFile(getBaseContext());
objOfRegistrationDetailsDataBaseFile.setName(getName);
Toast.makeText(getBaseContext(), "name", Toast.LENGTH_LONG).show();
}
});
}
}
You didn't define any type of the columns. There is also spacing problems. To create table modify your query statement as following:
private static final String QUERY="CREATE TABLE "+TABLE_NAME+" (id integer primary key autoincrement, "+NAME+" TEXT NOT NULL, "+PASSWORD+" TEXT NOT NULL, "+PHONE+" TEXT NOT NULL, "+ADDRESS+"TEXT NOT NULL);";
Also modify the onUpgrade() method as following:
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion)
{
db.execSQL("drop table if exists "+TABLE_NAME);
onCreate(db);
}

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();

Android - Can't create instance of Database

I am following this tutorial made by our folks over at Google.
If you look at the third code snippet, the tutorial shows the FeedReaderDbHelper class which extends from the SQLiteOpenHelper class.
public class FeedReaderDbHelper extends SQLiteOpenHelper
Then, it shows how to create an instance of the FeedReaderDbHelper object in order to use it to read and write to the database.
FeedReaderDbHelper mDbHelper = new FeedReaderDbHelper(getContext());
The problem as I see it is that SQLiteOpenHelper is an abstract class. So whenever I do the following in my own program:
MatchEntry.MatchDBHelper dbHelper = new MatchEntry.MatchDBHelper(context);
I get an error: ....MatchContract.MatchEntry is not an enclosing class
Is this because of SQLiteOpenHelper being abstract? The way I have set-up my classes are exactly the same and pretty much mimic exactly what the docs have.
Update
This is how the structure looks like so far:
public class MatchContract {
public MatchContract() {
}
public static abstract class MatchEntry implements BaseColumns {
...
...
...
}
public class MatchDBHelper extends SQLiteOpenHelper {
...
...
...
}
}
Am I correct to put the MatchDBHelper inside the MatchContract class? I assume so as it needs to know the SQL_CREATE_TABLE string, otherwise it won't know.
The idea is, that a Contract class is an abstraction of the database, So, in MatchContract you will have all the global variables of the database and in the inner class, MatchEntry you will implement the abstraction of the database itself (columns etc). Then, with MatchDBHelper you will be managing the database (Creating it, upgrading it etc) And writing SQL entries.
As I said in the comments, You need to create the helper class as a normal class, not an inner class:
public class MatchContract {
public MatchContract() {
}
/// Global variables for the DB
public static abstract class MatchEntry implements BaseColumns {
// Structure of the database
}
}
public class MatchDBHelper extends SQLiteOpenHelper {
// Manage the database
}
As a complete example:
public final class PersonContract {
public PersonContract() {
}
public static abstract class PersonEntry implements BaseColumns{
public static final String TABLE_NAME = "person";
public static final String COLUMN_NAME_ENTRY_ID = "personID";
public static final String COLUMN_NAME_FIRST_NAME = "firstname";
public static final String COLUMN_NAME_SECOND_NAME = "secondname";
}
}
public class PersonDbBHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "Persons.db";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + PersonEntry.TABLE_NAME + " (" +
PersonEntry._ID + " INTEGER PRIMARY KEY," + // Heredado de BaseColumns
PersonEntry.COLUMN_NAME_ENTRY_ID + TEXT_TYPE + COMMA_SEP +
PersonEntry.COLUMN_NAME_FIRST_NAME + TEXT_TYPE + COMMA_SEP +
PersonEntry.COLUMN_NAME_SECOND_NAME + TEXT_TYPE +
" )";
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + PersonEntry.TABLE_NAME;
public PersonDbBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
#Override
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}

SQLite database problems

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.

Categories

Resources