我尝试过不同的代码但不起作用。我的完整代码如下。
public class CheckData extends AppCompatActivity
{
private static final String enter_name="Name";
private static final String enter_email="Email_id";
private static final String enter_mob="Mobile";
private static final String enter_song="Song_rqst";
private static final String id="id";
private static final String database_path="Context.getFilesDir().getPath()";
private static final String database_name="sql_choice.db";
private static final String table_name="check_rank";
private static final int version = 2;
private DbHelper dbhelper;
private final Context ourcontext;
private SQLiteDatabase sqldatabase;
public CheckData(Context c)
{
//super();
ourcontext=c;
}
/* public CheckData()
{
// For the error I used this as solution but gives error
} */
public class DbHelper extends SQLiteOpenHelper
{
public DbHelper(Context context) {
super(context, database_name, null, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
if(isDBExist())
{
}
else
{
String execute="CREATE TABLE " + table_name +"(" +
id + " " + "INTEGER PRIMARY KEY ," + enter_name +" TEXT NOT NULL ," + enter_email
+ " TEXT NOT NULL , " + enter_mob + " INTEGER ,"+enter_song+" TEXT NOT NULL);";
db.execSQL(execute);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS" + database_name);
onCreate(db);
}
public boolean isDBExist()
{
SQLiteDatabase sql=null;
String path=database_path + database_name;
try
{
sql= SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
}
catch(Exception e)
{
}
if(sql !=null)
{
sql.close();
}
return sql !=null ? true :false;
}
public void openDatabase()
{
String path=database_path + database_name;
try
{
sqldatabase = SQLiteDatabase.openDatabase(path, null, SQLiteDatabase.OPEN_READWRITE);
}
catch (Exception e)
{
}
}
}
public CheckData open() throws SQLException
{
dbhelper =new DbHelper(ourcontext);
sqldatabase=dbhelper.getWritableDatabase();
dbhelper.openDatabase();
return this;
}
public void close()
{
dbhelper.close();
}
public long createEntry(String name, String email, String mob, String song)
{
ContentValues cv=new ContentValues();
cv.put(enter_name, name);
cv.put(enter_email,email );
cv.put(enter_mob, mob);
cv.put(enter_song, song);
return sqldatabase.insert(table_name, null, cv);
}
public String getData()
{
String column[]={id,enter_name,enter_email,enter_mob,enter_song};
Cursor c=sqldatabase.query(table_name, column, null, null, null, null, null, null);
String result="";
int iid=c.getColumnIndex(id);
int iname=c.getColumnIndex(enter_name);
int iemail=c.getColumnIndex(enter_email);
int imob=c.getColumnIndex(enter_mob);
int isong=c.getColumnIndex(enter_song);
for(c.moveToFirst();!c.isAfterLast();c.moveToNext())
{
result=result + " " + c.getString(iid) + " " +c.getString(iname) +" " +c.getString(iemail) + "" +
c.getString(imob) +" " +c.getString(isong) + "" +
"\n";
}
return result;
}
}
任何人都可以解决这个问题吗?以上只有在生成签名的apk时才会出错。
错误:此类应提供默认构造函数(不带参数的公共构造函数)