This question already has answers here:
When does SQLiteOpenHelper onCreate() / onUpgrade() run?
(15 answers)
Closed 4 years ago.
I created a database named as memberInfo.db in android studio. but the file cannot be found in the device file explorer. I am using android studio 3.1.3. I attached a image of my device file explorer.
DBHelper.java class
public static final String DATABASE_NAME = "memberInfo.db";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
String SQL_CREATE_ENTRIES = "CREATE TABLE " +
MembersMaster.Members.TABLE_NAME + " (" +
MembersMaster.Members.COLUMN_NAME_MEMBER_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
MembersMaster.Members.COLUMN_NAME_FULLNAME + " TEXT," +
MembersMaster.Members.COLUMN_NAME__ADDRESS + " TEXT," +
MembersMaster.Members.COLUMN_NAME__PHONE + " TEXT," +
MembersMaster.Members.COLUMN_NAME__EMAIL + "TEXT," +
MembersMaster.Members.COLUMN_NAME__BIRTHDATE + "TEXT)";
sqLiteDatabase.execSQL(SQL_CREATE_ENTRIES);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
}
}
MembersMaster.java class
package com.heisenberg.librarywiz;
import android.provider.BaseColumns;
public final class MembersMaster {
private MembersMaster() {};
public static class Members implements BaseColumns{
public static final String TABLE_NAME = "members";
public static final String COLUMN_NAME_MEMBER_ID = "memId";
public static final String COLUMN_NAME_FULLNAME = "fullname";
public static final String COLUMN_NAME__ADDRESS ="address";
public static final String COLUMN_NAME__PHONE ="phone";
public static final String COLUMN_NAME__EMAIL ="email";
public static final String COLUMN_NAME__BIRTHDATE ="birthdate";
}
}
try this in SQL_CREATE_ENTIRES:
String SQL_CREATE_ENTRIES = "CREATE TABLE " +
MembersMaster.Members.TABLE_NAME + " (" +
MembersMaster.Members.COLUMN_NAME_MEMBER_ID + " INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, " +
MembersMaster.Members.COLUMN_NAME_FULLNAME + " TEXT, " +
MembersMaster.Members.COLUMN_NAME__ADDRESS + " TEXT, " +
MembersMaster.Members.COLUMN_NAME__PHONE + " TEXT, " +
MembersMaster.Members.COLUMN_NAME__EMAIL + "TEXT, " +
MembersMaster.Members.COLUMN_NAME__BIRTHDATE + "TEXT);";
You left semicolon at the end of expression.
Another tip:
public static class Members implements BaseColumns{
public static final String TABLE_NAME = "members";
public static final String COLUMN_NAME_MEMBER_ID = BaseColumns._ID; // if you implement BaseColumns, the table IS should use this
public static final String COLUMN_NAME_FULLNAME = "fullname";
public static final String COLUMN_NAME__ADDRESS ="address";
public static final String COLUMN_NAME__PHONE ="phone";
public static final String COLUMN_NAME__EMAIL ="email";
public static final String COLUMN_NAME__BIRTHDATE ="birthdate";
}
For db name try to use only lowercase letters like "member_info.db".
The db should be created in the following directory:
/data/data/your_package_name/databases/member_info.db
Please let me know if it works (or not...)
Related
I have 2 table Employee and order table, I'm trying to implement those tables into the same databasehelper class, but is gives me several errors, should i create a another databasehelper class, or can i implement these to into one
tables are looks like this
`public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "shopinstyle.db";
private static final String DB_TABLE = "Employee";
private static final String ORDER_TABLE = "Order";
//Employee
//columns
private static final String ID = "ID";
private static final String FNAME = "FNAME";
private static final String LNAME = "LNAME";
private static final String PNUMBER = "PNUMBER";
private static final String EMAIL = "EMAIL";
private static final String NIC = "NIC";
//Order
//columns
private static final String ord_ID = "ord_ID";
private static final String ord_Name = "ord_Name";
private static final String ord_Qty = "ord_Qty";
private static final String ord_Price = "ord_Price";
private static final String ord_Location = "ord_Location";
private static final String CREATE_TABLE = "CREATE TABLE " + DB_TABLE + " (" +
ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
FNAME + " TEXT, " +
LNAME + " TEXT, " +
PNUMBER + " TEXT, " +
EMAIL + " TEXT, " +
NIC + " TEXT" + ")";
private static final String CREATE_TABLE_ORDER = "CREATE TABLE " + ORDER_TABLE + " (" +
ord_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
ord_Name + " TEXT, " +
ord_Qty + " TEXT, " +
ord_Price + " TEXT, " +
ord_Location + " TEXT " + ")";
public DatabaseHelper(Context context) {
super(context, DB_NAME,ORDER_TABLE, null, 1);
}
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
sqLiteDatabase.execSQL(CREATE_TABLE);
sqLiteDatabase.execSQL(CREATE_TABLE_ORDER);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + DB_TABLE);
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + ORDER_TABLE);
onCreate(sqLiteDatabase);
}`
there is a error in this line
public DatabaseHelper(Context context) {
super(context, DB_NAME,ORDER_TABLE, null, 1);
}
The 3d argument in the cal of super() inside the constructor of the DatabaseHelper class is factory which you can pass as null and not a table's name:
super(context, DB_NAME,null, 1);
I Have Total 5 Table in SQLite database So if i create a all table in DatabaseHalper.class and perform all CRUD((Create, Read, Update and Delete)) Operation in this class then this is look like a Big Data
Table Is Like: Company, Contact, ToDo. etc
So 1st I Want to create a multiple table in DatabaseHalper.class
2nd Create a another class Like using Table Name ComapnyDB.class
In this Class i want to Perform CRUD operation for a Company Table also All query perform for Company table
3rd same for Contact table. Create table in DatabaseHalper.class
and create a new class like Contact.class in this class perform all CURD operation and perform other operation
So this way my code was Divided in different different class
For Example Below class is my DatabseHalper.class and in this class i'll create a Database Tables.
DatabaseHalper.class
public class DatabaseHelper extends SQLiteOpenHelper {
// Logcat tag
private static final String LOG = "DatabaseHelper";
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "contactsManager";
//Tables Name
private static final String COMPANY = "company";
private static final String CONTACTS = "contacts";
private static final String EVENT = "event";
private static final String TODO = "todo";
private static final String USER = "user";
// Common column names
private static final String KEY_ID = "id";
//Companies Table Column Name
private static final String KEY_COMPANY_ID = "CompanyId";
private static final String KEY_COMPANY_REFERENCE_ID = "ReferenceId";
private static final String KEY_COMPANY_NAME = "CompanyName";
private static final String KEY_COMPANY_WEBSITE = "CompanyWebsite";
private static final String KEY_COMPANY_EMAIL = "CompanyEmail";
private static final String KEY_COMPANY_PHONE_HOME = "CompanyPhoneHome";
private static final String KEY_COMPANY_PHONE_PRIMARY = "CompanyPhonePrimary";
private static final String KEY_COMPANY_ADDRESS1 = "CompanyAddress";
private static final String KEY_COMPANY_ADDRESS2 = "CompanyAddressSecondary";
private static final String KEY_COMPANY_CITY = "CompanyCity";
private static final String KEY_COMPANY_STATE = "CompanyState";
private static final String KEY_COMPANY_ZIP = "CompanyZip";
private static final String KEY_COMPANY_COUNTRY = "CompanyCountry";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Table Create Statements
// Todo table create statement
private static final String CREATE_TABLE_COMPANY = "CREATE TABLE "
+ COMPANY + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_COMPANY_ID + " INTEGER,"
+ KEY_COMPANY_REFERENCE_ID + " INTEGER,"
+ KEY_COMPANY_NAME + " VARCHAR,"
+ KEY_COMPANY_WEBSITE + "VARCHAR,"
+ KEY_COMPANY_EMAIL + "VARCHAR,"
+ KEY_COMPANY_PHONE_HOME + "VARCHAR,"
+ KEY_COMPANY_PHONE_PRIMARY + "VARCHAR,"
+ KEY_COMPANY_ADDRESS1 + "VARCHAR,"
+ KEY_COMPANY_ADDRESS2 + "VARCHAR,"
+ KEY_COMPANY_CITY + "VARCHAR,"
+ KEY_COMPANY_STATE + "VARCHAR,"
+ KEY_COMPANY_ZIP + "INTEGER,"
+ KEY_COMPANY_COUNTRY + "VARCHAR" + ")";
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE_COMPANY);
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + COMPANY);
}
}
Now I'll Create CompanyDB.class For a CURD Operation But How to i access DatabaseHelper.class In Company.class.
Thanks In Advance :)
The following is an example of splitting tables away from the DatabaseHelper :-
ContactDB.java
public class ContactDB {
public static final String TBNAME = "contact";
public static final String COL_ID = BaseColumns._ID;
public static final String COL_NAME = "contact_name";
public static final String COL_EMAIL = "contact_email";
public static String getTableCreatSQL() {
return "CREATE TABLE IF NOT EXISTS " + TBNAME + "(" +
COL_ID + " INTEGER PRIMARY KEY, " +
COL_NAME + " TEXT," +
COL_EMAIL + " TEXT" +
")";
}
public static long addContact(SQLiteDatabase db, String contact_name, String contact_email) {
ContentValues cv = new ContentValues();
cv.put(COL_NAME,contact_name);
cv.put(COL_EMAIL,contact_email);
return db.insert(TBNAME,null,cv);
}
public static Cursor getAllContacts(SQLiteDatabase db) {
return db.query(TBNAME,null,null,null,null,null,COL_NAME + " ASC," + COL_EMAIL + " ASC");
}
}
CompanyDB.java
public class CompanyDB {
public static final String TBNAME = "company";
public static final String COL_ID = BaseColumns._ID;
public static final String COL_NAME = "company_name";
public static String getTableCreateSQL() {
return "CREATE TABLE IF NOT EXISTS " + TBNAME + "(" +
COL_ID + " INTEGER PRIMARY KEY," +
COL_NAME + " TEXT" +
")";
}
public static long addCompany(SQLiteDatabase db, String company_name ) {
ContentValues cv = new ContentValues();
cv.put(COL_NAME,company_name);
return db.insert(TBNAME,null,cv);
}
public static Cursor getAllCompanies(SQLiteDatabase db) {
return db.query(TBNAME,null,null,null,null,null,COL_NAME + " ASC");
}
}
DBHelper.java (Database helper)
public class DBHelper extends SQLiteOpenHelper {
public static final String DBNAME = "mydatabase";
public static final int DBVERSION = 1;
private static SQLiteDatabase mDB;
public DBHelper(Context context) {
super(context, DBNAME, null, DBVERSION);
mDB = this.getWritableDatabase();
}
#Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CompanyDB.getTableCreateSQL());
db.execSQL(ContactDB.getTableCreatSQL());
}
#Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
Cursor mAllContacts;
Cursor mAllCompanies;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SQLiteDatabase mDB = new DBHelper(this).getWritableDatabase();
CompanyDB.addCompany(mDB,"mycompany");
ContactDB.addContact(mDB,"Fred","Fred#email.com");
ContactDB.addContact(mDB,"Bert","bertbloggins#bloggings.moc");
mAllCompanies = CompanyDB.getAllCompanies(mDB);
while (mAllCompanies.moveToNext()) {
Log.d("COMPANY",
"Company Name = " +
mAllCompanies.getString(
mAllCompanies.getColumnIndex(
CompanyDB.COL_NAME
)
)
);
}
Cursor mAllContacts = ContactDB.getAllContacts(mDB);
while (mAllContacts.moveToNext()) {
Log.d("CONTACT",
"Contact Name = " +
mAllContacts.getString(
mAllContacts.getColumnIndex(
ContactDB.COL_NAME
)
) +
" Email = " +
mAllContacts.getString(
mAllContacts.getColumnIndex(
ContactDB.COL_EMAIL
)
)
);
}
}
#Override
protected void onDestroy() {
super.onDestroy();
mAllCompanies.close();
mAllContacts.close();
}
}
This will add a company and 2 contacts (each time that it is run), retrieve the companies and contacts from the database and write the details to the log.
Output would be lime (first run) :-
04-02 09:09:42.556 1497-1497/so49607475_splittableclasses.so49607475_splittableclasses D/COMPANY: Company Name = mycompany
04-02 09:09:42.556 1497-1497/so49607475_splittableclasses.so49607475_splittableclasses D/CONTACT: Contact Name = Bert Email = bertbloggins#bloggings.moc
Contact Name = Fred Email = Fred#email.com
I am trying to update the value of a particular column in my table. I already have a ContentProvder set up. I am receiving an error and I don't know how to fix it. I have searched for help on-line and none of them seem to update based on the unique-id. Here is my code..
#Override
public int update (Uri uri, ContentValues values, String selection,
String[] selectionArgs) {
int uriType = NewsContract.sURI_MATCHER.match(uri);
SQLiteDatabase db = dictionaryHelper.getWritableDatabase();
int rowsUpdated = 0;
String id = null;
switch (uriType)
{case DictionaryContract.RECENT:
rowsUpdated = db.update(DictionaryContract.DictionaryDataContract.RECENT_TABLE_NAME,
values,
selection,
selectionArgs);
break;
case DictionaryContract.RECENT_ID:
id = uri.getLastPathSegment();
if (TextUtils.isEmpty(selection)) {
rowsUpdated = db.update(DictionaryContract.DictionaryDataContract.RECENT_TABLE_NAME,
values,
DictionaryContract.DictionaryDataContract._ID + "=" + id,
null);
} else {
rowsUpdated = db.update(DictionaryContract.DictionaryDataContract.RECENT_TABLE_NAME,
values,
DictionaryContract.DictionaryDataContract._ID + "=" + id
+ " and "
+ selection, selectionArgs);
}
break;
default:
throw new IllegalArgumentException("Unknown URI" + uri);
}
getContext().getContentResolver().notifyChange(uri, null);
return rowsUpdated;
}
And this is how I am calling the update method of the ContentProvider:
private boolean UpdateRecentTable()
{
cursor.moveToLast();
Long id = cursor.getLong(cursor.getColumnIndex(DictionaryContract.DictionaryDataContract._ID));
Log.i("Update", " " + id);
String filter = DictionaryContract.DictionaryDataContract._ID + "=" + id;
ContentValues args = new ContentValues();
args.put(DictionaryContract.DictionaryDataContract.REC_FAVORITED, 1);
dResolver.update(DictionaryContract.CONTENT_URI_RECENT, args, filter, null);
return true;
}
Error:
03-21 04:05:36.823 18326-18673/com.example.clinton.light E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.clinton.light, PID: 18326
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:309)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.IllegalArgumentException: Unknown URIcontent://com.example.clinton.light.provider.dictionary/recents
at com.example.clinton.light.database.DictionaryContentProvider.update(DictionaryContentProvider.java:216)
at android.content.ContentProvider$Transport.update(ContentProvider.java:355)
at android.content.ContentResolver.update(ContentResolver.java:1362)
at com.example.clinton.light.dictionary_main.StoreFavWord.UpdateRecentTable(StoreFavWord.java:72)
at com.example.clinton.light.dictionary_main.StoreFavWord.doInBackground(StoreFavWord.java:41)
at com.example.clinton.light.dictionary_main.StoreFavWord.doInBackground(StoreFavWord.java:18)
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
03-21 04:05:37.271 18326-18352/com.example.clinton.light E/Surface: getSlotFromBufferLocked: unknown buffer: 0xe0c79230
DataContract
ublic class DictionaryContract {
public DictionaryContract(){}
public static final int TODAY = 100;
public static final int TODAY_ID = 115;
public static final int FAVORITE = 120;
public static final int FAVORITE_ID = 125;
public static final int RECENT = 130;
public static final int RECENT_ID = 135;
private static final String AUTHORITY = "com.example.clinton.light.provider.dictionary";
public static final String TODAY_BASE_PATH = "today";
public static final String FAVORITE_BASE_PATH = "favorites";
public static final String RECENT_BASE_PATH = "recents";
public static final UriMatcher sURI_MATCHER = new UriMatcher(UriMatcher.NO_MATCH);
static {
sURI_MATCHER.addURI(AUTHORITY, TODAY_BASE_PATH, TODAY);
sURI_MATCHER.addURI(AUTHORITY, TODAY_BASE_PATH + "/#", TODAY_ID);
sURI_MATCHER.addURI(AUTHORITY, FAVORITE_BASE_PATH, FAVORITE);
sURI_MATCHER.addURI(AUTHORITY, FAVORITE_BASE_PATH + "/#", FAVORITE_ID);
sURI_MATCHER.addURI(AUTHORITY, RECENT_BASE_PATH, RECENT);
sURI_MATCHER.addURI(AUTHORITY, RECENT_BASE_PATH + "/#", RECENT_ID);
}
public static final Uri CONTENT_URI_TODAY = Uri.parse("content://" + AUTHORITY + "/" + TODAY_BASE_PATH);
public static final Uri CONTENT_URI_FAVORITES = Uri.parse("content://" + AUTHORITY + "/" + FAVORITE_BASE_PATH);
public static final Uri CONTENT_URI_RECENT = Uri.parse("content://" + AUTHORITY + "/" + RECENT_BASE_PATH);
public static abstract class DictionaryDataContract implements BaseColumns
{
public static final String TODAY_TABLE_NAME = "todaytable";
public static final String TODAY_DATE = "worddate";
public static final String WORD = "wordname";
public static final String DEFINITION = "worddefinition";
public static final String SPEECH = "wordspeech";
public static final String ROOT = "wordroot";
public static final String EXAMPLE = "wordexample";
private static final String CREATE_TODAY_TABLE = "create table "+
TODAY_TABLE_NAME +
"(" +
_ID +
" integer primary key autoincrement, " +
TODAY_DATE +
" text not null, " +
WORD +
" text not null, " +
DEFINITION +
" text not null, " +
EXAMPLE +
" text not null, " +
SPEECH +
" text not null, " +
ROOT +
" text not null);";
public static final String FAVORITE_TABLE_NAME = "favoritetable";
public static final String FAV_WORD = "wordname";
public static final String FAV_DEFINITION = "worddefinition";
public static final String FAV_DEFINITION2 = "worddefinition2";
public static final String FAV_SPEECH = "wordspeech";
public static final String FAV_SPEECH2 = "wordspeech2";
public static final String FAV_RELATED = "wordrelated";
public static final String FAV_EXAMPLE = "wordexample";
public static final String RECENT_TABLE_NAME = "recentetable";
public static final String REC_WORD = "wordname";
public static final String REC_DEFINITION = "worddefinition";
public static final String REC_DEFINITION2 = "worddefinition2";
public static final String REC_SPEECH = "wordspeech";
public static final String REC_SPEECH2 = "wordspeech2";
public static final String REC_RELATED = "wordrelated";
public static final String REC_EXAMPLE = "wordexample";
public static final String REC_FAVORITED = "favorited";
private static final String CREATE_FAVORITE_TABLE = "create table "+
FAVORITE_TABLE_NAME +
"(" +
_ID +
" integer primary key autoincrement, " +
FAV_WORD +
" text not null, " +
FAV_DEFINITION +
" text not null, " +
FAV_DEFINITION2 +
" text not null, " +
FAV_EXAMPLE +
" text not null, " +
FAV_SPEECH +
" text not null, " +
FAV_SPEECH2 +
" text not null, " +
FAV_RELATED +
" text not null);";
private static final String CREATE_RECENT_TABLE = "create table "+
RECENT_TABLE_NAME +
"(" +
_ID +
" integer primary key autoincrement, " +
REC_WORD +
" text not null, " +
REC_DEFINITION +
" text not null, " +
REC_DEFINITION2 +
" text not null, " +
REC_EXAMPLE +
" text not null, " +
REC_SPEECH +
" text not null, " +
REC_SPEECH2 +
" text not null, " +
REC_FAVORITED +
" int not null, " +
REC_RELATED +
" text not null);";
public static void onCreate (SQLiteDatabase db) {
db.execSQL(CREATE_TODAY_TABLE);
db.execSQL(CREATE_FAVORITE_TABLE);
db.execSQL(CREATE_RECENT_TABLE);
}
public static void onUpgrade (SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TODAY_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + FAVORITE_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + RECENT_TABLE_NAME);
onCreate(db);
}
}
}
package com.mytelco.ahliang125.mytelco;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by ahliang125 on 4/29/2015.
*/
public class DBHelper extends SQLiteOpenHelper {
//DatabaseRecord
public static final String DATABASE_CREATE_RECORD = "create table " +
DatabaseRecord.DATABASE_TABLE_RECORD + " ("
+ DatabaseRecord.KEY_ID +
" integer primary key autoincrement, " +
DatabaseRecord.NAME + " text not null, " +
DatabaseRecord.TITLE + " text not null, " +
DatabaseRecord.LINK + " text not null," +
DatabaseRecord.REMARK + " text not null, " +
DatabaseRecord.PRIORITY + " text not null, " +
DatabaseRecord.USERNAME + " text not null);";
//DatabaseRecord
public static final String DATABASE_CREATE_USER = "create table " +
DatabaseUser.DATABASE_TABLE_USER + " ("
+ DatabaseUser.KEY_ID +
" integer primary key autoincrement, " +
DatabaseUser.USERNAME + " text not null, " +
DatabaseUser.PASSWORD + " text not null);";
//version number to upgrade database version
//each time if you Add, Edit table, you need to change the
//version number.
public static final int DATABASE_VERSION = 3;
//DatabaseRecord Table
public static final String DATABASE_TABLE_RECORD = "myTelco_databaseRecord";
// DatabaseRecord Name
public static final String DATABASE_NAME_RECORD = "myTelco_databaseRecord";
//DatabaseUser Table
public static final String DATABASE_TABLE_USER = "myTelco_databaseUser";
// DatabaseUser Name
public static final String DATABASE_NAME_USER = "myTelco_databaseUser";
//DatabaseRecord path
public static final String DATABASE_PATH = "/data/data/com.example.ahliang125.mytelco/databases/";
public static final String KEY_ID = "_id";
public static final String NAME = "name";
public static final String TITLE = "title";
public static final String LINK = "link";
public static final String REMARK = "remark";
public static final String PRIORITY = "priority";
public static final String USERNAME = "username";
public static final String PASSWORD = "password";
/*public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}*/
#Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {
//All necessary tables you like to create will create here
sqLiteDatabase.execSQL(DATABASE_CREATE_RECORD);
sqLiteDatabase.execSQL(DATABASE_CREATE_USER);
}
#Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i2) {
sqLiteDatabase.execSQL("DROP TABLE IF EXISTS "
+ DatabaseRecord.DATABASE_TABLE_RECORD);
onCreate(sqLiteDatabase);
}
public DBHelper(Context context) {
super(context, "myTelco_database.db", null, DATABASE_VERSION);
}
/* //---opens the database---
public DBHelper open() {
db = DBHelper.getWritableDatabase();
return this;
}
//---closes the database---
public void close() {
DBHelper.close();
}*/
}
This happens when i try to sign my apk to upload to the google play store. Google for few hours and tried the solutions but its still the same error. HELP!I know that there are no default constructor for sqliteopenhelper and we need to call the explicit constructor.
It's an Android Lint error that validates the classes that are declared in the manifests as entry points such as activities or services. Entry points must have a no-arg constructor.
Your DBHelper is not an entry point and should not be declared in the manifest.
Lint by default is run only on release builds or when you explicitly run it. That's why you get it when exporting a release version of your app.
I think this happened because you don't have constructor from super class http://developer.android.com/reference/android/database/sqlite/SQLiteOpenHelper.html
Try to add SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) and SQLiteOpenHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version, DatabaseErrorHandler errorHandler)
public DBHelper(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
super(context, name, factory, version);
}
And the same for the other constructor
I've encountered an error with creating and loading my database for my program. I get an error of "no such column: responsible: , while compiling:
SELECT id, name, responsible, priority
FROM tasks "
Can someone point out what I've done wrong. Thanks
public class TasksSQLiteOpenHelper extends SQLiteOpenHelper {
public static final int VERSION = 1;
public static final String DB_NAME = "tasks_db.sqlite";
public static final String TASKS_TABLE = "tasks";
public static final String TASK_ID = "id";
public static final String TASK_NAME = "name";
public static final String TASK_COMPLETE = "complete";
public static final String TASK_RESPONSIBLE = "responsible";
public static final String TASK_PRIORITY = "priority";
public TasksSQLiteOpenHelper(Context context) {
super(context, DB_NAME, null, VERSION);
}
#Override
public void onCreate(SQLiteDatabase db) {
dropAndCreate(db);
}
protected void dropAndCreate(SQLiteDatabase db) {
db.execSQL("drop table if exists " + TASKS_TABLE + ";");
createTables(db);
}
protected void createTables(SQLiteDatabase db) {
db.execSQL(
"create table " + TASKS_TABLE +" (" +
TASK_ID + " integer primary key autoincrement not null," +
TASK_NAME + " text," +
TASK_COMPLETE + " text," +
TASK_RESPONSIBLE + " text" +
TASK_PRIORITY + " integer" +
");"
);
}
}
-
public class TaskManagerApplication extends Application {
private SQLiteDatabase database;
private ArrayList<Task> currentTasks;
#Override
public void onCreate() {
super.onCreate();
TasksSQLiteOpenHelper helper = new TasksSQLiteOpenHelper(this);
database = helper.getWritableDatabase();
if (null == currentTasks) {
loadTasks();
}
}
private void loadTasks() {
currentTasks = new ArrayList<Task>();
Cursor tasksCursor = database.query(TASKS_TABLE, new String[] {
TASK_ID,
TASK_NAME,
TASK_RESPONSIBLE,
TASK_PRIORITY,
TASK_COMPLETE}, null, null, null, null, String.format("%s,%s", TASK_COMPLETE, TASK_NAME));
tasksCursor.moveToFirst();
Task t;
if (! tasksCursor.isAfterLast()) {
do {
int id = tasksCursor.getInt(0);
String name = tasksCursor.getString(1);
String priority = tasksCursor.getString(2);
String responsible = tasksCursor.getString(3);
String boolValue = tasksCursor.getString(4);
boolean complete = Boolean.parseBoolean(boolValue);
t = new Task(name, priority, responsible);
t.setId(id);
t.setComplete(complete);
currentTasks.add(t);
} while (tasksCursor.moveToNext());
}
tasksCursor.close();
}
}
You have a typo in your CREATE TABLE;
db.execSQL(
"create table " + TASKS_TABLE +" (" +
TASK_ID + " integer primary key autoincrement not null," +
TASK_NAME + " text," +
TASK_COMPLETE + " text," +
TASK_RESPONSIBLE + " text" + // <--- missing a comma
TASK_PRIORITY + " integer" +
");"
);
You have a minor typing error in your code. Corrected version :
db.execSQL(
"create table " + TASKS_TABLE +" (" +
TASK_ID + " integer primary key autoincrement not null," +
TASK_NAME + " text," +
TASK_COMPLETE + " text," +
TASK_RESPONSIBLE + " text," +
TASK_PRIORITY + " integer" +
");"
);