application has stopped after running the application - android

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

Related

Android SQLiteOpenHelper : SOPL from onCreate() method not shown

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

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

Please why do I get the error "SQLiteException: no such table: tabella (code1)?

Please why do I get the error "SQLiteException: no such table: tabella (code1)???
from LogCat I get: "SQLiteException: no such table: tabella (code1)" when I try to access the database fro the activity.
Here is the SQLiteOpenHelper code:
public class DatabaseHelper extends SQLiteOpenHelper {
private static final String CREATE_DB_SQL = "CREATE TABLE tabella (_id INTEGER PRIMARY KEY AUTOINCREMENT, colonna1 TEXT, colonna2 TEXT, colonna3 TEXT);";
public DatabaseHelper(Context context) {
super(context, "basedati", null, 1);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_DB_SQL);
ContentValues cv = new ContentValues (3);
cv.put("colonna1", "valore colonna 1");
cv.put("colonna2", "valore colonna 3");
cv.put("colonna3", "valore colonna 3");
db.insert("tabella", "colonna1", cv);
}
#Override
public void onUpgrade(SQLiteDatabase db, int arg1, int arg2) {
db.execSQL("DROP TABLE IF EXISTS tabella");
onCreate(db);
}
}
Here is the Activity code in which I try to open the database:
public class MainActivity extends Activity {
private DatabaseHelper db=null;
private Cursor constantsCursor=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = new DatabaseHelper(this);
constantsCursor=db.getReadableDatabase().rawQuery("SELECT colonna1 FROM tabella ORDER BY colonna1", null);
}
}
Odds are that you have changed the table after running your app at least once. You cannot simply change the Java String CREATE_DB_SQL, you must also update the SQL schema. The easiest way to do this is to increment the version number:
public DatabaseHelper(Context context) {
super(context, "basedati", null, 2); // Change the number to two
}

db.insert only stores auto incremented id in database

db.insert function inserts only an auto incremented ID in the table and nothing else though I get values from activity edit texts. Rest of the cells of table row remain empty
Here is my code:
public class SHelper extends SQLiteOpenHelper {
static final String dbName = "School";
static final String studentTable = "Student";
static final String colName = "Name";
static final String colPassword = "Password";
public static final int dbVersion=1;
public SHelper(Context context)
{
super(context, dbName, null, dbVersion);
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY, "+colName+" TEXT,"+colPassword+" TEXT);");
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + studentTable);
onCreate(db);
}
public void add(StudentData studentdata)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(colName, StudentData.getName());
values.put(colPassword, StudentData.getPassword());
db.insert(studentTable, null, values);
db.close();
}
}
public class StudentData {
private static String name;
private static String password;
public StudentData() {
super();
}
public StudentData(String name,String password) {
super();
StudentData.name = name;
StudentData.password = password;
}
public static String getName() {
return name;
}
public void setName(String name) {
StudentData.name = name;
}
public static String getPassword() {
return password;
}
public void setPassword(String password) {
StudentData.password = password;
}
}
public class stoActivity extends Activity {
SHelper db = new SHelper(this);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sto);
final EditText fullname=(EditText)findViewById(R.id.fullName);
final EditText password=(EditText)findViewById(R.id.password);
final String fullname=fullname.getText().toString();
final String password=password.getText().toString();
Button registerbutton = (Button) findViewById(R.id.registerButton);
registerbutton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
db.add(new StudentData(fullname,password));
Intent it=new Intent(stoActivity.this,otherActivity.class);
stoActivity.this.startActivity(it);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_register, menu);
return true;
}
}
There's your issue:
public void add(*StudentData studentdata)
{
...
values.put(colName, *StudentData.getName());
values.put(colPassword, *StudentData.getPassword());
...
}
Can you see it? Your statement is referring to the wrong part of the signature. you need to change the StudentData in the body of your method to lower case studentdata to match what is being passed to the method as a parameter. This illegal behavior went by compiled because you have the static modifier in places where i'm not sure is necessary, ex. the getName() and getPassword() methods.
create table with AutoIncrement key, so change method onCreate to the following:
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE "+studentTable+"("+colID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+colName+" TEXT,"+colPassword+" TEXT);");
}
You are not inserting any value for the column you have declared as primary Key.
It´s just an assumtion, but I had similar problems if I wanted to save data inside db. In your onCreate-Method at db.execSQL, You got a blank after comma --> "INTEGER PRIMARY KEY, ". I know that SQLite is very sensitive with such kind of input, You have to look exactly at blanks, commas or any other input.

alter table code in android...

following is the code that i used to alter my table but my app is crashing.
I have create this dbupdateadapter class separately. the records are being added, but the column is not added to the same table
public class dbupdateadapter {
public static final String TABLE_MEMBERID="memberid";
private static final String DATABASE_NAME="myfemmefab.db";
private static final String TABLE_NAME="registeration";
private static final int DATABASE_VERSION=2;
private static final String DATABASE_UPDATE="ALTER TABLE registeration ADD COLUMN(memberid TEXT);";
private static Context context1;
private dbhelper1 helper1;
private SQLiteDatabase sdb1;
public dbupdateadapter(Context con){
this.context1=con;
helper1=new dbhelper1(context1);
}
public class dbhelper1 extends SQLiteOpenHelper{
public dbhelper1(Context context){
super(context,DATABASE_NAME,null,DATABASE_VERSION);
}
#Override
public void onCreate(SQLiteDatabase sdb1){
sdb1.execSQL(DATABASE_UPDATE);
}
#Override
public void onUpgrade(SQLiteDatabase sdb1,int oldver,int newver){
//android.util.Log.w("constants", "upgrading database will destroy the old data");
if(newver>oldver){
sdb1.execSQL(DATABASE_UPDATE);
}
//onCreate(sdb1);
}
}
public dbupdateadapter open() throws SQLException{
sdb1=helper1.getWritableDatabase();
return this;
}
void insertcoldata (String memberid){
ContentValues cv1=new ContentValues();
cv1.put(TABLE_MEMBERID, memberid);
sdb1.insert(TABLE_NAME, null, cv1);
}
public void close(){
helper1.close();
}
}
You should know that in
public void onCreate(SQLiteDatabase db ){ ... }
you should create your tables and populate them with some initial values and you didn't create table in your snippet of code.
for upgrating your tables you should use
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { ... }
Exactly from docs:
Called when the database needs to be upgraded. The implementation
should use this method to drop tables, add tables, or do anything else
it needs to upgrade to the new schema version.
Also have look at
SQL As Understood By
SQLite
Example of how to implement ALTER
TABLE
Use "ALTER TABLE registeration ADD memberid TEXT;"; or "ALTER TABLE registeration ADD COLUMN memberid TEXT;"; You have an error in syntax.
Where are you creating yout table? You are executing DATABASE_UPDATE in your onCreate().

Categories

Resources