是的,我已經閱讀了許多許多答案,並嘗試了所有,但沒有爲我工作。我不知道什麼是錯的,因爲根據我的看法,一切都很好。表沒有列名爲-Sqlite - android - 我的第一個應用程序
很多時候我改變了DB版本。即使我重命名數據庫和表名稱,但沒有任何作品。我可以打印顯示正確結果的日誌,但爲什麼它沒有插入到數據庫中。
也當我從表中刪除所有複選框字段然後數據插入工作。
一個不起眼的request..please不從詢問的問題,因爲這問題是別人問了很多次,我嘗試了所有的解決方案,但沒有奏效阻止我。
請指導我,因爲我對Java和Android Studio完全陌生。
有時,錯誤的是:
table tblCinFormNew has no column named chkbox_other
有時,錯誤的是:
table tblCinFormNew has no column named chkbox_cosmo
這裏是我的數據庫處理器類:
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "dbCinExtDb";
private static final String TABLE_CONTACTS = "tblCinFormNew";
private static final String KEY_ID = "id";
private static final String KEY_EMAIL = "email";
private static final String KEY_PH_NO = "phone_number";
private static final String KEY_WORK_PH_NO = "work_phone_number";
private static final String KEY_FNAME = "fname";
private static final String KEY_LNAME = "lname";
private static final String KEY_ADDRESS = "address";
private static final String KEY_CITY = "city";
private static final String KEY_STATE = "state";
private static final String KEY_ZIP = "zip";
private static final String KEY_SALOON = "chkbox_saloon";
private static final String KEY_COSMO = "chkbox_cosmo";
private static final String KEY_STUDENT = "chkbox_student";
private static final String KEY_OTHER = "chkbox_other";
private static final String KEY_ADD_INFO = "chkbox_add_info";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = " CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_EMAIL + " TEXT,"
+ KEY_PH_NO + " TEXT," + KEY_WORK_PH_NO + " TEXT, " + KEY_FNAME + " TEXT, "
+ KEY_LNAME + " TEXT," + KEY_ADDRESS + " TEXT,"+ KEY_CITY + " TEXT,"
+ KEY_STATE + " TEXT," + KEY_ZIP + " TEXT " + KEY_SALOON + " TEXT " + KEY_COSMO + " TEXT " + KEY_STUDENT
+ " TEXT " + KEY_OTHER + " TEXT " + KEY_ADD_INFO + " TEXT " + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
//db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS_NEW);
// Create tables again
onCreate(db);
}
void addContactNew(Contact contact) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_EMAIL, contact.getEmail());
values.put(KEY_PH_NO, contact.getPhone());
values.put(KEY_WORK_PH_NO, contact.getWorkPhone());
values.put(KEY_FNAME, contact.getFirstName()); // Contact Name
values.put(KEY_LNAME, contact.getlastName());
values.put(KEY_ADDRESS, contact.getAddress());
values.put(KEY_CITY, contact.getCity());
values.put(KEY_STATE, contact.getState());
values.put(KEY_ZIP, contact.getZip());
values.put(KEY_SALOON, contact.getSaloon());
values.put(KEY_COSMO, contact.getCosmo());
values.put(KEY_STUDENT, contact.getStudent());
values.put(KEY_OTHER, contact.getOther());
values.put(KEY_ADD_INFO, contact.getAddInfo());
// Inserting Row
db.insert(TABLE_CONTACTS, null, values);
db.close(); // Closing database connection
}}
這裏是我的MainActivity.Java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final DatabaseHandler db = new DatabaseHandler(this);
final ProgressDialog progress = new ProgressDialog(this);
Button btn= (Button) findViewById(R.id.btnSave);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText email = (EditText) findViewById(R.id.editTextEmail);
EditText phone = (EditText) findViewById(R.id.editTextPhone);
EditText work_phone = (EditText) findViewById(R.id.editTextPhone1);
EditText fname = (EditText) findViewById(R.id.editTextFirstName);
EditText lname = (EditText) findViewById(R.id.editTextLastName);
EditText address = (EditText) findViewById(R.id.editTextAddress);
EditText city = (EditText) findViewById(R.id.editTextCity);
EditText state = (EditText) findViewById(R.id.editTextState);
EditText zip = (EditText) findViewById(R.id.editTextZip);
CheckBox chkSaloon =(CheckBox) findViewById(R.id.checkBoxSalon);
CheckBox chkCosmo =(CheckBox) findViewById(R.id.checkBoxCosmetologist);
CheckBox chkStudent = (CheckBox) findViewById(R.id.checkBoxStudent);
CheckBox chkOther = (CheckBox) findViewById(R.id.checkBoxOther);
CheckBox chkAddInfo = (CheckBox)findViewById(R.id.checkBoxAdditionalInfo);
String _email=email.getText().toString();
String _phone=phone.getText().toString();
String _work_phone=work_phone.getText().toString();
String _fname=fname.getText().toString();
String _lname=lname.getText().toString();
String _address=address.getText().toString();
String _city=city.getText().toString();
String _state=state.getText().toString();
String _zip=zip.getText().toString();
String strSaloon;
String strCosmo;
String strStudent;
String strOther;
String strAdInfo;
if(chkSaloon.isChecked()) {
strSaloon="Salon owner";
}
else{
strSaloon="Not Checked";
}
if(chkCosmo.isChecked()) {
strCosmo="Licensed Cosmetologist";
}
else{
strCosmo="Not Checked";
}
if(chkStudent.isChecked()) {
strStudent="Student";
}
else{
strStudent="Not Checked";
}
if(chkOther.isChecked()) {
strOther="Other";
}
else{
strOther="Not Checked";
}
if(chkAddInfo.isChecked()) {
strAdInfo="Please send me additional information";
}
else{
strAdInfo="Not Checked";
}
Log.d("Insert: ", "Inserting ..");
db.addContactNew(new Contact(_email,_phone,_work_phone,_fname,_lname,_address,_city,_state,_zip,
strSaloon,strCosmo,strStudent,strOther,strAdInfo));
// Reading all contacts
Log.d("Reading Email",_email);
Log.d("Reading Phone",_phone);
Log.d("Reading Work Phone",_work_phone);
Log.d("Reading Fname",_fname);
Log.d("Reading Lname",_lname);
Log.d("Reading Address",_address);
Log.d("Reading City",_city);
Log.d("Reading State",_state);
Log.d("Reading Zip",_zip);
Log.d("Reading Saloon",strSaloon);
Log.d("Reading Cosmo",strCosmo);
Log.d("Reading Student",strStudent);
Log.d("Reading Other",strOther);
Log.d("Reading AddInfo",strAdInfo);
// Log.d("GetContactCount",String.valueOf(db.getContactsCount()));
Log.d("Reading: ", "Reading all contacts..");
List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
String log = "Id: "+cn.getID() +" ,Name: " + cn.getFirstName() + cn.getFirstName() + " ,Saloon: " + cn.getSaloon()
+ " ,Cosmo: " + cn.getCosmo();
// Writing Contacts to log
Log.d("Name: ", log);
}
}
});
}
}
這裏是我的Contact.java構造
public class Contact {
int id;
String email;
String phone;
String workPhone;
String firstName;
String lastName;
String address;
String city;
String state;
String zip;
String chkSalon;
String chkCosmo;
String chkStudent;
String chkOther;
String chkAddInfo;
public Contact()
{
// Empty Constructor
}
public Contact(int id, String email, String phone, String workPhone,String firstName,
String lastName, String address,String city , String state,String zip, String chkSalon,
String chkCosmo, String chkStudent, String chkOther,String chkAddInfo
){
this.id = id;
this.email = email;
this.phone = phone;
this.workPhone=workPhone;
this.firstName=firstName;
this.lastName=lastName;
this.address=address;
this.city=city;
this.state=state;
this.zip=zip;
this.chkSalon=chkSalon;
this.chkCosmo=chkCosmo;
this.chkStudent=chkStudent;
this.chkOther=chkOther;
this.chkAddInfo=chkAddInfo;
}
// constructor
public Contact(String email, String phone, String workPhone,String firstName,
String lastName, String address,String city , String state,String zip, String chkSalon,
String chkCosmo, String chkStudent, String chkOther,String chkAddInfo
){
this.email = email;
this.phone = phone;
this.workPhone=workPhone;
this.firstName=firstName;
this.lastName=lastName;
this.address=address;
this.city=city;
this.state=state;
this.zip=zip;
this.chkSalon=chkSalon;
this.chkCosmo=chkCosmo;
this.chkStudent=chkStudent;
this.chkOther=chkOther;
this.chkAddInfo=chkAddInfo;
}
// getting ID
public int getID(){
return this.id;
}
// setting id
public void setID(int id){
this.id = id;
}
public String getPhone(){
return this.phone;
}
public void setPhone(String phone){
this.phone = phone;
}
public String getWorkPhone(){
return this.workPhone;
}
public void setWorkPhone(String workPhone){
this.workPhone = workPhone;
}
public String getEmail(){
return this.email ;
}
public void setEmail(String email){
this.email = email;
}
public String getFirstName(){
return this.firstName;
}
public void setFirstName(String firstName){
this.firstName=firstName;
}
public String getlastName(){
return this.lastName;
}
public void setLastName(String lastName){
this.lastName=lastName;
}
public void setAddress(String address){
this.address=address;
}
public String getAddress(){
return this.address;
}
public String getCity(){
return this.city;
}
public void setCity(String city){
this.city=city;
}
public String getState(){
return this.state;
}
public void setState(String state){
this.state=state;
}
public String getZip(){ return this.zip; }
public void setZip(String zip){
this.zip=zip;
}
public void setSaloon(String chkSalon){
this.chkSalon=chkSalon ;
}
public String getSaloon(){ return this.chkSalon; }
public String getCosmo(){ return this.chkCosmo;}
public void setCosmo(String chkCosmo){
this.chkCosmo=chkCosmo ;
}
public String getStudent(){
return this.chkStudent;
}
public void setStudent(String chkStudent){
this.chkStudent=chkStudent ;
}
public String getOther(){
return this.chkOther;
}
public void setOther(String chkOther){
this.chkOther=chkOther ;
}
public String getAddInfo(){
return this.chkAddInfo;
}
public void setAddInfo(String chkAddInfo){
this.chkAddInfo=chkAddInfo ;
}
}
你是我的英雄!非常感謝Ankur指出了這個問題,我錯過了:D。順便說一句,我也嘗試過ADM,但它顯示空白下的文件夾和沒有數據路徑。我也在我的本地計算機adm上執行了shell腳本,但是它說每次Permission Denied。 – vanurag