다음과 같은 문제가 있습니다. thenewboston Android tut의커서 또는 ContentValues가 내 SQLite 데이터를 읽을 수 없습니까? : D
첫 번째 문제는 내가 정확히 완료 한 작업을 수행했음을 확신하지만 이번에는 내 앱이 작동하지 않습니다.
미안 내 게시물이 너무 오래 될 경우 당신은 분명히 내 질문을 할 수 있다면, 당신은 IT를 편집 할 수 있습니다
그래서 내가 활동 연장 SQLiteExample 클래스입니다 :
public class SQLiteExample extends Activity implements OnClickListener{
Button sqlUpdate, sqlView;
EditText sqlName, sqlHotness;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
sqlUpdate = (Button) findViewById(R.id.bSqlUpdate);
sqlView = (Button) findViewById(R.id.bSqlopenView);
sqlName = (EditText) findViewById(R.id.etSqlName);
sqlHotness = (EditText) findViewById(R.id.etSqlHot);
sqlUpdate.setOnClickListener(this);
sqlView.setOnClickListener(this);
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.bSqlUpdate :
boolean didItWork = true;
try {
String name = sqlName.getText().toString();
String hotness = sqlHotness.getText().toString();
HotOrNot entry = new HotOrNot(SQLiteExample.this);
entry.open();
entry.createEntry(name, hotness);
entry.close();
} catch (Exception e) {
// TODO Auto-generated catch block
didItWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Dang It!");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(didItWork){
Dialog d = new Dialog(this);
d.setTitle("Heck Yea!");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.bSqlopenView :
Intent i = new Intent("com.thenewboston.tama.SQLView");
startActivity(i);
break;
}
}
나는 단지 setOnClickListener가 ()을 2 버튼에 연결합니다.
이
가 개최 내 HotOrNot 클래스 인 SQLite는 물건의 :public class HotOrNot {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "persons_name";
public static final String KEY_HOTNESS = "persons_hotness";
private static final String DATABASE_NAME = "HotOrNotdb";
private static final String DATABASE_TABLE = "peopleTable";
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_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_NAME + " TEXT NOT NULL, " +
KEY_HOTNESS + "TEXT NOT NULL); "
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public HotOrNot(Context c){
ourContext = c;
}
//?????????????????????
public HotOrNot open() throws SQLException{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close(){
ourHelper.close();
}
public long createEntry(String name, String hotness) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name);
cv.put(KEY_HOTNESS, hotness);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_ROWID, KEY_NAME, KEY_HOTNESS};
Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
String result = "";
int iRow = c.getColumnIndex(KEY_ROWID);
int iName = c.getColumnIndex(KEY_NAME);
int iHotness = c.getColumnIndex(KEY_HOTNESS);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()){
result += c.getString(iRow) + " " + c.getString(iName) + " " +c.getString(iHotness) + "\n";
}
return result;
}
그리고 이것은 (2 버튼에 대한) 내 SQLView 확장 활동 :
public class SQLView extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlview);
TextView tv = (TextView) findViewById(R.id.tvSQLinfo);
HotOrNot info = new HotOrNot(this);
info.open();
String data = info.getData();
info.close();
tv.setText(data);
}
그리고이 오류입니다 난 내 SQLiteExample 클래스에서 R.id.bSqlopenView 단추를 클릭했을 때 :
03-19 14:47:54.504: I/Database(335): sqlite returned: error code = 1, msg = table peopleTable has no column named persons_hotness
이 내 정식 버전 오류 목록 : D
03-19 14:47:28.194: D/dalvikvm(335): GC_EXTERNAL_ALLOC freed 50K, 53% free 2554K/5379K, external 1625K/2137K, paused 107ms
03-19 14:47:49.634: W/KeyCharacterMap(335): No keyboard for id 0
03-19 14:47:49.634: W/KeyCharacterMap(335): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
03-19 14:47:54.504: I/Database(335): sqlite returned: error code = 1, msg = table peopleTable has no column named persons_hotness
03-19 14:47:54.515: E/Database(335): Error inserting persons_hotness= persons_name=
03-19 14:47:54.515: E/Database(335): android.database.sqlite.SQLiteException: table peopleTable has no column named persons_hotness: , while compiling: INSERT INTO peopleTable(persons_hotness, persons_name) VALUES(?, ?);
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:65)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:83)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:41)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteDatabase.compileStatement(SQLiteDatabase.java:1149)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1569)
03-19 14:47:54.515: E/Database(335): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1426)
03-19 14:47:54.515: E/Database(335): at com.thenewboston.tama.HotOrNot.createEntry(HotOrNot.java:70)
03-19 14:47:54.515: E/Database(335): at com.thenewboston.tama.SQLiteExample.onClick(SQLiteExample.java:46)
03-19 14:47:54.515: E/Database(335): at android.view.View.performClick(View.java:2485)
03-19 14:47:54.515: E/Database(335): at android.view.View$PerformClick.run(View.java:9080)
03-19 14:47:54.515: E/Database(335): at android.os.Handler.handleCallback(Handler.java:587)
03-19 14:47:54.515: E/Database(335): at android.os.Handler.dispatchMessage(Handler.java:92)
03-19 14:47:54.515: E/Database(335): at android.os.Looper.loop(Looper.java:123)
03-19 14:47:54.515: E/Database(335): at android.app.ActivityThread.main(ActivityThread.java:3683)
03-19 14:47:54.515: E/Database(335): at java.lang.reflect.Method.invokeNative(Native Method)
03-19 14:47:54.515: E/Database(335): at java.lang.reflect.Method.invoke(Method.java:507)
03-19 14:47:54.515: E/Database(335): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-19 14:47:54.515: E/Database(335): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-19 14:47:54.515: E/Database(335): at dalvik.system.NativeStart.main(Native Method)
기존 데이터베이스를 삭제하고 다시 시도하십시오. 데이터베이스가 이미 생성 된 후에 ** persons_hotness ** 테이블을 추가하려고 시도했지만 실패했을 수 있습니다. – waqaslam
감사합니다, 선생님,하지만 내 데이터베이스를 다시 만드는 방법? 나는 새로운 에뮬레이터를 만들려고했는데 (내 데이터베이스를 자동으로 다시 만들 것이라고 생각하지만 여전히 작동하지 않습니다 : D) 그리고 people_hotness 테이블을 추가한다는 것은 무엇을 의미합니까? "peopleTable"이라는 테이블이 하나 있습니다. 죄송합니다. 이해가 잘 안되면 방금 SQLite를 배웠습니다. 감사합니다. 선생님 : D –
죄송합니다. ** 열 * ** persons_hotness **를 의미합니다. – waqaslam