赤ちゃんの身長と体重の増加率を計算して保存するボタンと、名前と誕生日を保存するボタンがあります。
しかし、私のコードは強制終了を引き起こします...
私のデータベースJava:
public class MySQLiteHelper extends SQLiteOpenHelper {
//Table name
public static final String TABLE_BABY = "Baby_table";
//Table columns
public static final String COLUMN_NAME = "_name";
public static final String COLUMN_BIRTH_DATE = "birth_date";
public static final String COLUMN_INITIAL_HEIGHT = "initail_height";
public static final String COLUMN_INITIAL_WEIGHT = "initail_weight";
//Table name
public static final String TABLE_MEASUREMENTS = "Measurements_table";
//Table columns
public static final String COLUMN_HEIGHT = "_height";
public static final String COLUMN_WEIGHT = "_weight";
public static final String COLUMN_NUM = "_num";
public static final String COLUMN_BABY_NAME = "_Babyname";
//Database file name
private static final String DATABASE_NAME = "Baby.db";
//path
private static String DB_PATH = "";
//Database version
private static final int DATABASE_VERSION = 1;
static final String viewBaby ="ViewBaby";
//Constructor
public MySQLiteHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
/** DB_PATH = "/data/data/"
+ context.getApplicationContext().getPackageName()
+ "/databases/"; */
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_MEASUREMENTS + "("
+ COLUMN_NUM+ " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_HEIGHT + "Integer," +COLUMN_WEIGHT+ "Integer,"+COLUMN_BABY_NAME+"TEXT NOT NULL ,FOREIGN KEY ("+ COLUMN_BABY_NAME +") REFERENCES "+TABLE_BABY+" ("+ COLUMN_NAME +"));");
db.execSQL("CREATE TABLE" + TABLE_BABY + "(" +COLUMN_NAME +"TEXT PRIMARY KEY," + COLUMN_INITIAL_HEIGHT +" Integer," + COLUMN_INITIAL_WEIGHT+"Integer,"+COLUMN_BIRTH_DATE +"DATE)");
//db.execSQL(DATABASE_CREATE);
db.execSQL("CREATE TRIGGER fk_babyname_measurmentname1" +
" BEFORE INSERT "+
" ON "+TABLE_MEASUREMENTS+
" FOR EACH ROW BEGIN"+
" SELECT CASE WHEN ((SELECT "+ COLUMN_NAME +" FROM "+TABLE_BABY+
" WHERE "+COLUMN_NAME+"=new."+COLUMN_BABY_NAME+" ) IS NULL)"+
" THEN RAISE (ABORT,'Foreign Key Violation') END;"+
" END;");
// Create view
db.execSQL("CREATE VIEW "+viewBaby+
" AS SELECT "+TABLE_MEASUREMENTS+"."+COLUMN_NUM+" AS _id,"+
" "+TABLE_MEASUREMENTS+"."+COLUMN_HEIGHT+","+
" "+TABLE_MEASUREMENTS+"."+COLUMN_WEIGHT+","+
" FROM "+TABLE_MEASUREMENTS+" JOIN "+TABLE_BABY+
" ON "+TABLE_MEASUREMENTS+"."+COLUMN_BABY_NAME+" ="+TABLE_BABY+"."+COLUMN_NAME
);
}
/** Called when the DATABASE_VERSION is changed to higher version */
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(MySQLiteHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + TABLE_BABY + TABLE_MEASUREMENTS);
db.execSQL("DROP VIEW IF EXISTS "+viewBaby);
onCreate(db);
}
//insert
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
}
/**
* @return
*/
public long getLastInsertId() {
long index = 0;
SQLiteDatabase sdb = getReadableDatabase();
Cursor cursor = sdb.query(
"sqlite_sequence",
new String[]{"seq"},
"name = ?",
new String[]{MySQLiteHelper.TABLE_MEASUREMENTS},
null,
null,
null,
null
);
if (cursor.moveToFirst()) {
index = cursor.getLong(cursor.getColumnIndex("seq"));
}
cursor.close();
return index;
}
public void insertWeightAndHeight(String column, Long value) {
SQLiteDatabase db=this.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put(column, value);
db.insert(MySQLiteHelper.TABLE_MEASUREMENTS, null,
initialValues);
// TODO Auto-generated method stub
}
public void open() throws SQLException {
// void
SQLiteDatabase database = this.getWritableDatabase();
//return dbHelper;
}
/** Used to close connection with database. */
public void close() {
//if(database != null)
//{database.close();}
this.close();
}
public void insertDate(Date birthdate) {
// TODO Auto-generated method stub
SQLiteDatabase database=this.getWritableDatabase();
ContentValues initialValues = new ContentValues();
/**Used to parse from Date to String and the other way around*/
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
initialValues.put(MySQLiteHelper.COLUMN_BIRTH_DATE, dateFormat.format(birthdate));
database.insert(MySQLiteHelper.TABLE_BABY, null,
initialValues);
}
public void insertName(String name) {
SQLiteDatabase database=this.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put(MySQLiteHelper.COLUMN_NAME, name);
database.insert(MySQLiteHelper.TABLE_BABY, null,
initialValues);
// TODO Auto-generated method stub
}
/** used to get Last height & weight from TABLE_MEASUREMENTS by Num.*/
@SuppressWarnings("null")
public static Long getLastHeight(){
@SuppressWarnings("unused")
SQLiteDatabase database=dpHelper.getWritableDatabase();
Cursor cursor = null;
int index = cursor.getColumnIndex(COLUMN_HEIGHT);
Measurements measurement = new Measurements();
MySQLiteHelper sqlliteHelper = null ;
Long i= sqlliteHelper.getLastInsertId();
String[] asColumnsToReturn = new String[] { COLUMN_HEIGHT};
cursor = database.query(TABLE_MEASUREMENTS,asColumnsToReturn, "COLUMN_NUM = i",null,null,null,null );
cursor.moveToFirst();
Long height = cursor.getLong(index);
return height;
}
/** used to get Last h n eight & weight from TABLE_MEASUREMENTS by Num.*/
@SuppressWarnings({ "null", "unused" })
public static Long getLastWeight(){
SQLiteDatabase database=dpHelper.getWritableDatabase();
Measurements measurement = new Measurements();
Cursor cursor = null;
int index =cursor.getColumnIndex(COLUMN_WEIGHT);
MySQLiteHelper sqlliteHelper = null ;
Long i= sqlliteHelper.getLastInsertId();
String[] asColumnsToReturn = new String[] { COLUMN_WEIGHT};
cursor = database.query(TABLE_MEASUREMENTS,asColumnsToReturn, "COLUMN_NUM = i",null,null,null,null );
cursor.moveToFirst();
Long weight = cursor.getLong(index);
return weight;
}
/** used to get Last h n eight & weight from TABLE_MEASUREMENTS by Num.*/
static MySQLiteHelper dpHelper;
public int caluclateAge() throws ParseException{
SQLiteDatabase database=dpHelper.getWritableDatabase();
String[] asColumnsToReturn = new String[] { MySQLiteHelper.COLUMN_BIRTH_DATE};
Cursor cursor = database.query(MySQLiteHelper.TABLE_BABY,asColumnsToReturn, null,null,null,null,null );
/**Used to parse from Date to String and the other way around*/
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
Date Birthdate = dateFormat.parse(cursor.getString(3));
Calendar dob = Calendar.getInstance();
dob.setTime(Birthdate);
Calendar today = Calendar.getInstance();
int age = today.get(Calendar.MONTH) - dob.get(Calendar.MONTH);
return age;
}
}
チャートアクティビティ:
public class ChartActivity extends Activity {
//private BabyAdapter adapter;
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final MySQLiteHelper db = new MySQLiteHelper(this);
final WebView googleChartView = new WebView(this);
db.open();
final Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText EditText3 =(EditText) findViewById(R.id.editText3);
final EditText EditText4 =(EditText) findViewById(R.id.editText4);
//BabyDataSource BDS = new BDS();
Long h =MySQLiteHelper.getLastHeight();
Long W =MySQLiteHelper.getLastWeight();
String h2;
String w2;
Long oneH ;
Long twoH ;
Long threeH ;
Long oneW ;
Long twoW ;
Long threeW ;
Long hPercent;
Long wPercent;
Long HV;
Long WV;
if (h == null && W == null){
hPercent =(long) 0.0 ;
wPercent =(long) 0.0;
h = (long)0.0;
W = (long)0.0;
h2 = EditText3.getText().toString();
w2 = EditText4.getText().toString();
HV = Long.parseLong(h2);
WV = Long.parseLong(w2);
db.insertWeightAndHeight(MySQLiteHelper.COLUMN_HEIGHT, HV);
db.insertWeightAndHeight(MySQLiteHelper.COLUMN_WEIGHT, WV);
} else {
h2 = EditText3.getText().toString();
w2 = EditText4.getText().toString();
HV = Long.parseLong(h2);
WV = Long.parseLong(w2);
oneH = HV-h;
twoH = oneH/h;
threeH = twoH *100;
hPercent = threeH ;
oneW = WV-W;
twoW = oneW/W;
threeW = twoW *100;
wPercent = threeW ;
db.insertWeightAndHeight(MySQLiteHelper.COLUMN_HEIGHT, HV);
db.insertWeightAndHeight(MySQLiteHelper.COLUMN_WEIGHT, WV);
}
String strH = hPercent.toString();
String strW = wPercent.toString();
EditText3.setText(strH +"%");
EditText4.setText(strW +"%");
//chart
setContentView(googleChartView);
String mUrl = "http://chart.apis.google.com/chart?chf=bg,s,245245245&chxs=0,FFFFFF00,9.5&chxt=x,y&chxl=0:Xheight|currentHeight|Xweight|currentWeight&chs=500x300&cht=bvg&chco=3072F3|FF9900|80C65A|990066&chd=t:"+h+","+HV+","+W+","+WV+"&chdl=50%%20X-Height|5%%20Current-Height|5%%20X-Weight|40%%20Current-Weight&chdlp=b&&chbh=10,5,5";
googleChartView.loadUrl(mUrl);
db.close();
// Perform action on click
}
});
final Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
final EditText EditText1;
final EditText EditText2;
EditText1 = (EditText) findViewById(R.id.editText1);
EditText2 = (EditText) findViewById(R.id.editText2);
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
String BirthDateStr = EditText2.getText().toString();
String name = EditText1.getText().toString();
Date Birthdate;
try {
Birthdate = dateFormat.parse(BirthDateStr);
int age = db.caluclateAge();
db.insertDate(Birthdate);
db.insertName(name);
EditText2.setText("your "+name+" is now "+age+"month old");
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
protected void onStop() {
super.onStop();
//Close dataSource
}
}
これはlogcatです:
05-14 12:23:43.095: I/Process(224): Sending signal. PID: 224 SIG: 9
05-14 12:28:10.824: D/dalvikvm(305): GC freed 555 objects / 48344 bytes in 92ms
05-14 12:28:11.424: E/Database(305): Failure 1 (near "TABLEBaby_table": syntax error) on 0x2d5de0 when preparing 'CREATE TABLEBaby_table(_nameTEXT PRIMARY KEY,birth_dateDATE)'.
05-14 12:28:11.434: D/AndroidRuntime(305): Shutting down VM
05-14 12:28:11.434: W/dalvikvm(305): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-14 12:28:11.434: E/AndroidRuntime(305): Uncaught handler: thread main exiting due to uncaught exception
05-14 12:28:11.454: E/AndroidRuntime(305): java.lang.RuntimeException: Unable to start activity ComponentInfo{chart.android.project/chart.android.project.ChartActivity}: android.database.sqlite.SQLiteException: near "TABLEBaby_table": syntax error: CREATE TABLEBaby_table(_nameTEXT PRIMARY KEY,birth_dateDATE)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.os.Handler.dispatchMessage(Handler.java:99)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.os.Looper.loop(Looper.java:123)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-14 12:28:11.454: E/AndroidRuntime(305): at java.lang.reflect.Method.invokeNative(Native Method)
05-14 12:28:11.454: E/AndroidRuntime(305): at java.lang.reflect.Method.invoke(Method.java:521)
05-14 12:28:11.454: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-14 12:28:11.454: E/AndroidRuntime(305): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-14 12:28:11.454: E/AndroidRuntime(305): at dalvik.system.NativeStart.main(Native Method)
05-14 12:28:11.454: E/AndroidRuntime(305): Caused by: android.database.sqlite.SQLiteException: near "TABLEBaby_table": syntax error: CREATE TABLEBaby_table(_nameTEXT PRIMARY KEY,birth_dateDATE)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.database.sqlite.SQLiteDatabase.native_execSQL(Native Method)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1610)
05-14 12:28:11.454: E/AndroidRuntime(305): at chart.android.project.MySQLiteHelper.onCreate(MySQLiteHelper.java:69)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:106)
05-14 12:28:11.454: E/AndroidRuntime(305): at chart.android.project.MySQLiteHelper.open(MySQLiteHelper.java:193)
05-14 12:28:11.454: E/AndroidRuntime(305): at chart.android.project.ChartActivity.onCreate(ChartActivity.java:34)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-14 12:28:11.454: E/AndroidRuntime(305): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-14 12:28:11.454: E/AndroidRuntime(305): ... 11 more
05-14 12:28:11.474: I/dalvikvm(305): threadid=7: reacting to signal 3
05-14 12:28:11.474: E/dalvikvm(305): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
新しいlogcat
05-15 20:27:47.319: W/dalvikvm(341): Exception Ljava/lang/NullPointerException; thrown during Lchart/android/project/MySQLiteHelper;.<clinit>
05-15 20:27:47.329: D/AndroidRuntime(341): Shutting down VM
05-15 20:27:47.339: W/dalvikvm(341): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-15 20:27:47.339: E/AndroidRuntime(341): Uncaught handler: thread main exiting due to uncaught exception
05-15 20:27:47.409: E/AndroidRuntime(341): java.lang.ExceptionInInitializerError
05-15 20:27:47.409: E/AndroidRuntime(341): at chart.android.project.ChartActivity.<init>(ChartActivity.java:27)
05-15 20:27:47.409: E/AndroidRuntime(341): at java.lang.Class.newInstanceImpl(Native Method)
05-15 20:27:47.409: E/AndroidRuntime(341): at java.lang.Class.newInstance(Class.java:1479)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.os.Handler.dispatchMessage(Handler.java:99)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.os.Looper.loop(Looper.java:123)
05-15 20:27:47.409: E/AndroidRuntime(341): at android.app.ActivityThread.main(ActivityThread.java:4363)
05-15 20:27:47.409: E/AndroidRuntime(341): at java.lang.reflect.Method.invokeNative(Native Method)
05-15 20:27:47.409: E/AndroidRuntime(341): at java.lang.reflect.Method.invoke(Method.java:521)
05-15 20:27:47.409: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-15 20:27:47.409: E/AndroidRuntime(341): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-15 20:27:47.409: E/AndroidRuntime(341): at dalvik.system.NativeStart.main(Native Method)
05-15 20:27:47.409: E/AndroidRuntime(341): Caused by: java.lang.NullPointerException
05-15 20:27:47.409: E/AndroidRuntime(341): at chart.android.project.MySQLiteHelper.<clinit>(MySQLiteHelper.java:46)
05-15 20:27:47.409: E/AndroidRuntime(341): ... 16 more
05-15 20:27:47.429: I/dalvikvm(341): threadid=7: reacting to signal 3
05-15 20:27:47.429: E/dalvikvm(341): Unable to open stack trace file '/data/anr/traces.txt': Permission denied