打开应用时,sqlite
出错。
Logcat错误:
java.lang.RuntimeException: Unable to start activity ComponentInfo{net.simplifiedcoding.androidmysqlsync/net.simplifiedcoding.androidmysqlsync.OneMore.MainActivity}: android.database.sqlite.SQLiteException: no such column: syncstatus (code
1): , while compiling: SELECT name, syncstatus FROM names at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
DbContact.java
package net.simplifiedcoding.androidmysqlsync.OneMore;
/**
* Created by Belal on 1/27/2017.
*/
public class DbContact {
public static final int SYNC_STATUS_OK = 0;
public static final int SYNC_STATUS_FAILED = 1;
//Constants for Database name, table name, and column item_view
public static final String DATABASE_NAME = "ddagroin_ddagro";
public static final String TABLE_NAME = "names";
public static final String NAME = "name";
public static final String SYNC_STATUS = "syncstatus";
public static final String COLUMN_ID = "id";
}
DbHelper.java
package net.simplifiedcoding.androidmysqlsync.OneMore;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Priya on 2/27/2017.
*/
public class DbHelper extends SQLiteOpenHelper {
//database version
private static final int DATABASE_VERSION = 1;
private static final String DROP_TABLE = "drop table if exists"+DbContact.TABLE_NAME;
public DbHelper(Context context){
super(context,DbContact.DATABASE_NAME,null,DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_TABLE= "CREATE TABLE " + DbContact.TABLE_NAME
+ "(" + DbContact.COLUMN_ID +
" INTEGER PRIMARY KEY AUTOINCREMENT, " + DbContact.NAME +
" VARCHAR, " + DbContact.SYNC_STATUS +
" TINYINT);";
db.execSQL(CREATE_TABLE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(DROP_TABLE);
onCreate(db);
}
public void saveToLocalDatabase(String name, int sync_status, SQLiteDatabase database){
ContentValues contentValues = new ContentValues();
contentValues.put(DbContact.NAME, name);
contentValues.put(DbContact.SYNC_STATUS, sync_status);
database.insert(DbContact.TABLE_NAME,null,contentValues);
}
public Cursor readFromLocalDatabase(SQLiteDatabase database){
String[] projection = {DbContact.NAME, DbContact.SYNC_STATUS};
return (database.query(DbContact.TABLE_NAME,projection,null,null,null,null,null));
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
RecyclerView recyclerView;
EditText Name;
RecyclerView.LayoutManager layoutManager;
RecyclerAdapter adapter;
ArrayList<Contact> arrayList = new ArrayList<>();
/*
* this is the url to our webservice
* make sure you are using the ip instead of localhost
* it will not work if you are using localhost
* */
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
recyclerView=(RecyclerView)findViewById(R.id.recyclerView);
Name=(EditText) findViewById(R.id.name);
layoutManager =new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
adapter = new RecyclerAdapter(arrayList);
recyclerView.setAdapter(adapter);
readFromLocalStorage();
}
public void submitName(View view){
String name = Name.getText().toString();
saveToLocalStorage(name);
Name.setText("");
}
private void readFromLocalStorage() {
arrayList.clear();
DbHelper dbHelper = new DbHelper(this);
SQLiteDatabase database = dbHelper.getReadableDatabase();
Cursor cursor = dbHelper.readFromLocalDatabase(database);
while (cursor.moveToNext()){
String name = cursor.getString(cursor.getColumnIndex(DbContact.NAME));
int sync_status=cursor.getInt(cursor.getColumnIndex(DbContact.SYNC_STATUS));
arrayList.add(new Contact(name,sync_status));
}
adapter.notifyDataSetChanged();
cursor.close();
dbHelper.close();
}
private void saveToLocalStorage(String name){
DbHelper dbHelper = new DbHelper(this);
SQLiteDatabase database = dbHelper.getWritableDatabase();
if(checkNetworkConnection()){
}else {
dbHelper.saveToLocalDatabase(name, DbContact.SYNC_STATUS_FAILED,database);
}
readFromLocalStorage();
dbHelper.close();
}
public boolean checkNetworkConnection(){
ConnectivityManager connectivityManager =(ConnectivityManager) this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo=connectivityManager.getActiveNetworkInfo();
return (networkInfo!= null && networkInfo.isConnected());
}
}
答案 0 :(得分:0)
这对我有用。您是否已添加该列并忘记更新DATABASE_VERSION
,请更新数据库版本并检查。
存在后留出空间&#34;:
public static final String DROP_TABLE = "DROP TABLE IF EXISTS " +DbContact.TABLE_NAME;
光标条目 你必须先选择列
String[] columns = {DbContact.NAME, DbContact.SYNC_STATUS};
Cursor cursor = db.query(yourtableName,
columns,
null,
null, null, null, null);
然后把你的while循环