Reputation: 1
SQLite database is not being created while no error coming.
I have checked the android/data folder trying many things. I am using eclipse. Someone kindly help me and thanks in advance.
My Database.java
file is this:
public class Database extends SQLiteOpenHelper {
public static final String DATABASE_NAME="Table.db";
public static final String TABLE_NAME="table_one";
public static final String COL_1="ID";
public static final String COL_2="NAME";
public static final String COL_3="SURNAME";
public static final String COL_4="MARKS";
public Database(Context context) {
super(context, TABLE_NAME, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase arg) {
// TODO Auto-generated method stub
arg.execSQL("create table "+TABLE_NAME+"(ID INTEGER PRIMARY KEY AUTOINCREMENT, NAME TEXT, SURNAME TEXT, MARKS INTEGER)");
}
@Override
public void onUpgrade(SQLiteDatabase arg, int arg1, int arg2) {
// TODO Auto-generated method stub
arg.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
onCreate(arg);
}
public boolean insert(String name, String sname, String marks)
{
SQLiteDatabase arg=this.getWritableDatabase();
ContentValues content=new ContentValues();
content.put(COL_2, name);
content.put(COL_3, sname);
content.put(COL_4, marks);
long result=arg.insert(TABLE_NAME, null, content);
if(result==-1)
return false;
else
return true;
}
}
MainActivity.java
is:
public class MainActivity extends Activity {
Database myDb;
EditText name, sname, marks;
Button add;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new Database(this);
name = (EditText) findViewById(R.id.ename);
sname = (EditText) findViewById(R.id.esname);
marks = (EditText) findViewById(R.id.emarks);
add = (Button) findViewById(R.id.button1);
addData();
}
public void addData() {
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
boolean isInserted = myDb.insert(name.getText().toString(), sname.getText().toString(), marks.getText().toString());
if (isInserted == true)
Toast.makeText(MainActivity.this, "Data is inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data is NOT inserted", Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Upvotes: 0
Views: 145
Reputation: 1247
Try taking a look here . You should however use a DAO class ; so you can manage information from the database and to it ,through an intermediate object.
Try creating the table like this :
private static final String DATABASE_CREATE = "create table "
+ DATABASE_NAME + "(" + COL_1 +
" integer primary key autoincrement, " +
COL_2 + " text not null, " + COL_3 + " text not null, " + COL_4 + " integer);";
Upvotes: 0
Reputation: 5451
public Database(Context context)
{
super(context, DATABASE_NAME , null, 1); // Your Mistake
}
Create your table like this :
String CREATE_TABLE = "CREATE TABLE " + GROUP_CHAT_MESSAGE_TABLE_NAME + "("
+ ID + " INTEGER PRIMARY KEY,"
+ NAME + " TEXT,"
+ SURNAME + " TEXT ,"
+ MARKS + " INTEGER" + ")";
db.execSQL(CREATE_TABLE);
Upvotes: 1