みなさん、sqlite dbにデータを挿入しようとしていますが、挿入ボタンをクリックしても、強制終了、エラー、成功/失敗のトーストメッセージは何も起こりません.そして、db情報の表示ボタンをクリックすると、データは挿入されません以前にこれを行って問題はありませんでしたが、今回はどこが間違っていたのかわかりません。助けてください。よろしくお願いします。
主な活動
package com.example.sql;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener {
Button insert,viewd;
EditText name,age;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = (EditText)findViewById(R.id.etname);
age = (EditText) findViewById(R.id.etage);
insert = (Button)findViewById(R.id.btninsert);
viewd = (Button)findViewById(R.id.btnview);
insert.setOnClickListener(this);
viewd.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
final MediaPlayer mpButtonClick = MediaPlayer.create(this, R.raw.buttonclick);
switch (v.getId()) {
case R.id.btninsert:
boolean status = true;
try{
String name1 = name.getText().toString();
String age1 = age.getText().toString();
SQLActivity entry = new SQLActivity(MainActivity.this);
entry.open();
entry.passDataToEntry(name1,age1);
entry.close();
mpButtonClick.start();
}
catch (Exception e) {
// TODO: handle exception
status=false;
String error = e.toString();
Log.e("Exception found ",error);
}
finally{
if(status)
{
Toast.makeText(getApplicationContext(), "Successfully Inserted", Toast.LENGTH_SHORT).show();
}
}
break;
case R.id.btnview:
Intent i = new Intent("com.example.sql.VIEW");
startActivity(i);
break;
}
}
}
SQLアクティビティ
package com.example.sql;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class SQLActivity {
public static final String KEY_ROWID = "_id";
public static final String KEY_NAME = "p_name";
public static final String KEY_AGE = "p_age";
private static final String DATABASE_NAME = "SQLDB";
private static final String DATABASE_TABLE = "data_table";
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_AGE + " TEXT NOT NULL);"
);
String y = "CREATE TABLE " + DATABASE_TABLE + " (" + KEY_ROWID
+ " INTEGER PRIMARY KEY AUTOINCREMENT , " + KEY_NAME + " TEXT NOT NULL , "
+ KEY_AGE + " TEXT NOT NULL);";
//System.out.println("query" + y);
Log.d("query", y);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w("TaskDBAdapter", "Upgrading from version " +
oldVersion + " to " +
newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public SQLActivity(Context c)
{
ourContext = c;
}
public SQLActivity open() throws SQLException
{
ourHelper = new DbHelper(ourContext);
ourDatabase = ourHelper.getWritableDatabase();
return this;
}
public void close()
{
ourHelper.close();
}
public long passDataToEntry(String name1, String age1) {
// TODO Auto-generated method stub
ContentValues cv = new ContentValues();
cv.put(KEY_NAME, name1);
cv.put(KEY_AGE, age1);
int i = (int)ourDatabase.insert(DATABASE_TABLE, null, cv);
String MY_TAG="";
Log.d(MY_TAG,"Value= "+i);
return ourDatabase.insert(DATABASE_TABLE, null, cv);
}
public String getData() {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_NAME,KEY_AGE};
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 iAge = c.getColumnIndex(KEY_AGE);
for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
{
result = result + c.getString(iName) + " " + c.getString(iAge) + "\n";
}
return result;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/tvinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="Age"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/etage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Button
android:id="@+id/btninsert"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="130dp"
android:text="Insert" />
<Button
android:id="@+id/btnview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View" />
</LinearLayout>
ログキャット
04-18 22:43:50.625: E/Zygote(33): setreuid() failed. errno: 2
04-18 22:43:56.095: E/Zygote(33): setreuid() failed. errno: 17
04-18 22:43:56.985: E/BatteryService(59): usbOnlinePath not found
04-18 22:43:56.985: E/BatteryService(59): batteryVoltagePath not found
04-18 22:43:56.985: E/BatteryService(59): batteryTemperaturePath not found
04-18 22:43:56.995: E/SurfaceFlinger(59): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
04-18 22:44:01.455: E/EventHub(59): could not get driver version for /dev/input/mouse0, Not a typewriter
04-18 22:44:01.455: E/EventHub(59): could not get driver version for /dev/input/mice, Not a typewriter
04-18 22:44:02.105: E/System(59): Failure starting core service
04-18 22:44:02.105: E/System(59): java.lang.SecurityException
04-18 22:44:02.105: E/System(59): at android.os.BinderProxy.transact(Native Method)
04-18 22:44:02.105: E/System(59): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
04-18 22:44:02.105: E/System(59): at android.os.ServiceManager.addService(ServiceManager.java:72)
04-18 22:44:02.105: E/System(59): at com.android.server.ServerThread.run(SystemServer.java:184)
04-18 22:44:02.665: E/SoundPool(59): error loading /system/media/audio/ui/Effect_Tick.ogg
04-18 22:44:02.675: E/SoundPool(59): error loading /system/media/audio/ui/KeypressStandard.ogg
04-18 22:44:02.675: E/SoundPool(59): error loading /system/media/audio/ui/KeypressSpacebar.ogg
04-18 22:44:02.675: E/SoundPool(59): error loading /system/media/audio/ui/KeypressDelete.ogg
04-18 22:44:02.675: E/SoundPool(59): error loading /system/media/audio/ui/KeypressReturn.ogg
04-18 22:44:03.755: E/ThrottleService(59): Could not open GPS configuration file /etc/gps.conf
04-18 22:44:05.005: E/logwrapper(147): executing /system/bin/tc failed: No such file or directory
04-18 22:44:05.045: E/logwrapper(148): executing /system/bin/tc failed: No such file or directory
04-18 22:44:05.065: E/logwrapper(149): executing /system/bin/tc failed: No such file or directory
04-18 22:44:10.423: E/HierarchicalStateMachine(59): TetherMaster - unhandledMessage: msg.what=3
アクティビティを見る
package com.example.sql;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ViewActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
TextView tv = (TextView)findViewById(R.id.tvinfo);
SQLActivity info = new SQLActivity(this);
info.open();
info.getData();
}
}
view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data...." />
<TextView
android:id="@+id/tvinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DB Info" />
</LinearLayout>