@Override public void onCreate(SQLiteDatabase db) { try{ db.execSQL(\"create table \" + NotificationManager.getUserSt
First of all I would recommend using android.util.Log for logging exceptions in Android.
Second - I suspect you have tables with wrong names created. Your error says query can't find "log", but I see you make some concatenation in "CREATE" statement. That may be the reason.
You can check what is actually created for you. By viewing the sqlite base created.
You can try:
adb shell
cd /data/data/<your.package.name>/databases
sqlite3 <yourdbname>
.tables
this will help you i have tried it and its also working
this is just an example
public class DatabaseMarks {
public static final String KEY_STUID = "stuid";
public static final String KEY_SUB1 = "subject_one";
public static final String KEY_SUB2 = "subject_two";
public static final String KEY_SUB3= "subject_three";
public static final String KEY_MARKS1= "marks_one";
public static final String KEY_MARKS2 = "marks_two";
public static final String KEY_MARKS3 = "marks_three";
private static final String DATABASE_NAME = "Student";
private static final String DATABASE_MARKSTABLE = "StudentMarks";
private static final int DATABASE_VERSION = 1;
private DbHelper ourHelper;
private final Context ourContext;
private SQLiteDatabase ourDatabase;
private static class DbHelper extends SQLiteOpenHelper{
public DbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(" CREATE TABLE " + DATABASE_MARKSTABLE + " (" +
KEY_STUID + " TEXT PRIMARY KEY, " +
KEY_SUB1 + " TEXT NOT NULL, " +
KEY_SUB2 + " TEXT NOT NULL, " +
KEY_SUB3 + " TEXT NOT NULL, " +
KEY_MARKS1 + " INTEGER NOT NULL, " +
KEY_MARKS2 + " INTEGER NOT NULL, " +
KEY_MARKS3 + " INTEGER NOT NULL);"
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MARKSTABLE);
onCreate(db);
}
}
public DatabaseMarks(Context c){
ourContext = c;
}
public DatabaseMarks open()throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createInsert(String stuid, String subject1, String subject2,
String subject3, String marks1, String marks2, String marks3) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_STUID, stuid);
cv.put(KEY_SUB1, subject1);
cv.put(KEY_SUB2, subject2);
cv.put(KEY_SUB3, subject3);
cv.put(KEY_MARKS1, marks1);
cv.put(KEY_MARKS2, marks2);
cv.put(KEY_MARKS3, marks3);
return ourDatabase.insert(DATABASE_MARKSTABLE, null, cv);
}
Remove DATABASE_NAME
part of your SQLs.
If you want NOPO
your database name, and want to create a table log
, then:
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, "NOPO", null, 2);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + "log" + " ("
+ KEY_TIME +" INTEGER primary key, "
+ KEY_TEXT +" TEXT not null);");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS log");
onCreate(db);
}
}
super(context, "NOPO", null, 2);
makes this DatabaseHelper object just in the NOPO
database environment, so you need not to specify it in SQL.
ps. I hardcode something and you should AVOID it.
public class DBHandler extends SQLiteOpenHelper {
private static String DATABASE_NAME = "mydb";
private static int DATABASE_VERSION = 1;
String TABLE_NAME = "Student";
String KEY_ID = "id";
String KEY_STUDENT_NAME = "name";
String KEY_STUDENT_MARKS = "marks";
public DBHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// creating table
String query = "CREATE TABLE " + TABLE_NAME +
" ( " +
KEY_ID + " INTEGER PRIMARY KEY," +
KEY_STUDENT_NAME + " TEXT," +
KEY_STUDENT_MARKS + " INTEGER"
" ) ";
db.execSQL(query);
// insertion in the table
ContentValues values = new ContentValues();
values.put(KEY_QUESTION_ID, "1");
values.put(KEY_STUDENT_NAME , "abc");
values.put(KEY_STUDENT_MARKS , "95");
db.insert(TABLE_NAME, null, values);
values.put(KEY_QUESTION_ID, "2");
values.put(KEY_STUDENT_NAME , "def");
values.put(KEY_STUDENT_MARKS , "93");
db.insert(TABLE_NAME, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
// Create tables again
onCreate(db);
}
// retrieve date
List<Student> getAllData(int Id) {
List<Student> student = new ArrayList<Student>();
// Select All Query
String selectQuery = "SELECT * FROM " + TABLE_NAME + " WHERE KEY_ID =" + "'" + Id + "'";
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
// looping through all rows and adding to list
if (cursor.moveToFirst()) {
do {
Student std = new Student();
u.setId(cursor.getInt(0));
u.setStudentName(cursor.getString(1));
u.setMarks(cursor.getString(2));
student.add(std);
} while (cursor.moveToNext());
}
// return student list
return student;
}
}
public class Student {
public static int Id;
public static String studentName;
public static int marks;
public int getId() {
return Id;
}
public static void setId(int id) {
Id = id;
}
public String getStudentName() {
return studentName;
}
public static void setStudentName(String studentName) {
Student.studentName = studentName;
}
public int getMarks() {
return marks;
}
public static void setMarks(int marks) {
Student.marks = marks;
}
}
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DBHandler db = new DBHandler(getApplicationContext());
List<Student> std = new ArrayList<Student>();
rl = db.getAllData(1);
for (Student s : std) {
Log.v("Id",s.getId());
Log.v("Name",s.getStudentName());
Log.v("Marks",s.getMarks());
}
}
}
public class LoginDataBaseAdapter { static final String DATABASE_NAME = "login.db";
static final int DATABASE_VERSION = 1;
public static final int NAME_COLUMN = 1;
// TODO: Create public field for each column in your table.
// SQL Statement to create a new database.
static final String DATABASE_CREATE = "create table "+"LOGIN"+
"( " +"ID"+" integer primary key autoincrement,"+ "USERNAME text,PASSWORD text,UNAME text); ";
// Variable to hold the database instance
public SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private DataBaseHelper dbHelper;
public LoginDataBaseAdapter(Context _context)
{
context = _context;
dbHelper = new DataBaseHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public LoginDataBaseAdapter open() throws SQLException
{
db = dbHelper.getWritableDatabase();
return this;
}
public void close()
{
db.close();
}
public SQLiteDatabase getDatabaseInstance()
{
return db;
}