0
我試圖用這個代碼寫的東西到DB:(從here了和編輯一點點)寫數據到數據庫
package com.example.db;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
String entered;
public void onClick (View v){
EditText et = (EditText)findViewById(R.id.et);
entered = et.getText().toString();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EditText et = (EditText)findViewById(R.id.et);
entered = et.getText().toString();
SQLiteDatabase myDB= null;
String TableName = "myTable";
String Data="";
try {
myDB = this.openOrCreateDatabase("DatabaseName", MODE_PRIVATE, null);
myDB.execSQL("CREATE TABLE IF NOT EXISTS "
+ TableName
+ " (Field1 STRING, Field2 INT(3));");
myDB.execSQL("INSERT INTO "
+ TableName
+ " (Field1, Field2)"
+ " VALUES ("+entered+", 22);");
Cursor c = myDB.rawQuery("SELECT * FROM " + TableName , null);
int Column1 = c.getColumnIndex("Field1");
int Column2 = c.getColumnIndex("Field2");
c.moveToFirst();
if (c != null) {
do {
String Name = c.getString(Column1);
int Age = c.getInt(Column2);
Data =Data +Name+"/"+Age+"\n";
}while(c.moveToNext());
}
TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(Data);
}
catch(Exception e) {
Log.e("Error", "Error", e);
} finally {
if (myDB != null)
myDB.close();
}
}
}
但它不工作。當我開始時,關閉程序。 哪裏是不正常的東西? 我該怎麼做?
Logcat,請。 http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto