Reputation: 13
This is my main activity and I'm getting exception at 29th line of insertData() in MainActivity method and MyDatabaseAdapter() class contains the schema.Can u please help me with where I'm getting the error?
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText editText;
Button button;
MyDatabaseAdapter myDatabaseAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText)findViewById(R.id.editText);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name=editText.getText().toString();
long id=myDatabaseAdapter.insertData(name);
if(id<0)
{
Toast.makeText(MainActivity.this,"Unsuccessful",Toast.LENGTH_LONG);
}
else
{
Toast.makeText(MainActivity.this,"Successfull",Toast.LENGTH_LONG).show();
}
}
});
}
}
This is my sqlite helper class
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;
public class MyDatabaseAdapter {
MyDatabase myDatabase;
MyDatabaseAdapter(Context context)
{
myDatabase=new MyDatabase(context);
}
public long insertData(String name)
{
SQLiteDatabase sqLiteDatabase=myDatabase.getWritableDatabase();
ContentValues contentValues=new ContentValues();
contentValues.put(MyDatabase.NAME,name);
long id=sqLiteDatabase.insert(myDatabase.TABLE_NAME,null,contentValues);
return id;
}
static class MyDatabase extends SQLiteOpenHelper
{
private static final String DATABASE_NAME="Mydatabase";
private static final int DATABASE_VERSION=3;
private static final String TABLE_NAME="MyContacts";
private static final String NAME="Name";
// private static final String ADDRESS="Address";
private static final String UID="_id";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+UID+" INTEGER PRIMARY KEY AUTOINCREMENT,"+NAME+" VARCHAR(255),;";
private static final String DROP_TABLE="DROP TABLE IF EXISTS" +TABLE_NAME;
public MyDatabase(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
Toast.makeText(context,"Constructor is called",Toast.LENGTH_LONG).show();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
}
}
Upvotes: 0
Views: 69
Reputation: 8490
Firstly, you should really be able to give us more information. Your logcat output should show exactly what the error is or run with the debugger to catch the exception as it happens. Anyhow, with the info provided, at least one problem is that your create statement has invalid syntax. It evaluates as:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255),;
The comma and semi-colon are not required and there is no closing bracket. It should be:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name VARCHAR(255))
Or more correctly:
CREATE TABLE MyContacts(_id INTEGER PRIMARY KEY AUTOINCREMENT,Name TEXT)
...because there is no VARCHAR type in SQLite. Type affinity converts your VARCHAR(255) to TEXT because it contains the string 'CHAR' but it is best to specify SQLite types only to ensure you get what you expect.
Upvotes: 1
Reputation: 11
Sorry if I'm talking something wrong, I'm still learning.
But in line 29, considering that you have 2 lines for thepackage and one break, this is the line:
shouldn't long be used for numbers ?
long id=myDatabaseAdapter.insertData(name);
"long: The long data type is a 64-bit two's complement integer. The signed long has a minimum value of -263 and a maximum value of 263-1. In Java SE 8 and later, you can use the long data type to represent an unsigned 64-bit long, which has a minimum value of 0 and a maximum value of 264-1. Use this data type when you need a range of values wider than those provided by int. The Long class also contains methods like compareUnsigned, divideUnsigned etc to support arithmetic operations for unsigned long."
Upvotes: 0
Reputation: 387
you did not instantiate your MyDatabaseAdapter class. Try
myDatabaseAdapter = new MyDatabaseAdapter(this) after setContentView().
Upvotes: 1