public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="Original";
private static final String TABLE_NAME="Register";
private static final String COLUMN_ID="id";
private static final String COLUMN_UNAME="name";
//private static final String COLUMN_EMAIL="email";
private static final String COLUMN_PHONENUMBER="number";
private static final String COLUMN_PASSWORD="password";
public DatabaseHelper(Context mcontext)
{
super(mcontext , DATABASE_NAME , null , DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
String CREATE_REGISTER_TABLE = " CREATE TABLE " + TABLE_NAME + " ( "
+ COLUMN_ID + " INTEGER PRIMARY KEY, "
+ COLUMN_UNAME + " TEXT, "
+ COLUMN_PHONENUMBER + " TEXT, "
+ COLUMN_PASSWORD + " TEXT " + " ) ";
db.execSQL(CREATE_REGISTER_TABLE);
}
public void insertContact(Original c)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
//String query="SELECT * FROM "+TABLE_NAME;
//Cursor cursor = db.rawQuery(query,null);
// int count=cursor.getCount();
values.put(COLUMN_ID,c.getID());
values.put(COLUMN_UNAME,c.getName());
//values.put(COLUMN_EMAIL,c.getEmail());
values.put(COLUMN_PASSWORD,c.getPassw());
values.put(COLUMN_PHONENUMBER,c.getNumber());
db.insert(TABLE_NAME , null , values);
db.close();
}
public String searchPass(String name)
{
SQLiteDatabase db = this.getReadableDatabase();
String query="SELECT "+COLUMN_UNAME + "AND " +COLUMN_PASSWORD +"FROM "+TABLE_NAME;
Cursor mCursor=db.rawQuery(query , null);
String a,b;
b="not found";
if(mCursor.moveToFirst())
{
do
{
a=mCursor.getString(0);
if(a.equals(name))
{
b=mCursor.getString(1);
break;
}
}
while(mCursor.moveToNext());
}
return b;
}
@Override
public void onUpgrade(SQLiteDatabase db, int i, int i1)
{
String query="DROP TABLE IF EXITS"+TABLE_NAME;
db.execSQL(query);
onCreate(db);
}
}
The Error is shown at the db.insert()
operation.
The main purpose is to add the details into the database name Original with the table name register
The code above is sort of OK, I have run it once (well twice using a single Original
instance to purposefully generate the UNIQUE constraint
error). However it will fail if the Original object's ID is not unique. You would get an error along the lines of :-
07-18 06:32:46.208 2819-2819/? E/SQLiteLog: (1555) abort at 11 in [INSERT INTO Register(number,name,password,id) VALUES (?,?,?,?)]: UNIQUE constraint failed: Register.id
07-18 06:32:46.208 2819-2819/? E/SQLiteDatabase: Error inserting number=0312341234 name=Fred password=password id=10
If this isn't the error, then perhaps it is because you have changed the table's structure (e.g. added(most likely)/removed columns) without deleting the database (either deleting the App's Data or uninstalling the App). This is a common mistake that people make as they don't realise that the onCreate
method is only automatically called when the database is created.
I suspect the latter as there is a sign that you have changed the table structure as per the commented out line //values.put(COLUMN_EMAIL,c.getEmail());
P.S. you should close the cursor in the searchPass
method before returning e.g. code mCursor.close();
If netiher of the above are the issue, then you should edit your question and include the log/stacktrace that details the error and also probably the code from the invoking activity e.g. (the code I used) :-
Original ori = new Original(10l,"Fred","password","fred@email.com","0312341234");
DatabaseHelper dbhlp = new DatabaseHelper(this);
dbhlp.insertContact(ori);