質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

Q&A

解決済

1回答

1710閲覧

取得した情報がデータベースに書き込まれません。

退会済みユーザー

退会済みユーザー

総合スコア0

SQLite

SQLiteはリレーショナルデータベース管理システムの1つで、サーバーではなくライブラリとして使用されている。

Android

Androidは、Google社が開発したスマートフォンやタブレットなど携帯端末向けのプラットフォームです。 カーネル・ミドルウェア・ユーザーインターフェイス・ウェブブラウザ・電話帳などのアプリケーションやソフトウェアをひとつにまとめて構成。 カーネル・ライブラリ・ランタイムはほとんどがC言語/C++、アプリケーションなどはJavaSEのサブセットとAndroid環境で書かれています。

Android Studio

Android Studioは、 Google社によって開発された、 Androidのネイティブアプリケーション開発に特化した統合開発ツールです。

0グッド

0クリップ

投稿2016/12/21 09:55

編集2016/12/22 04:18

###前提・実現したいこと
GPSの情報がデータベースのテーブルに入りません。
MainActivity.javaのMySQLiteHelper.writeGPS(MainActivity.this, out);から、MySQLiteHelpe.javaに送っています。
送り方、受け取り方、格納の仕方が間違っているのでしょうか?
どうすれば入るのか教えてください。
###該当のソースコード

MainActivity.java public class MainActivity extends AppCompatActivity { private LocationManager mLocationManager = null; private LocationListener mLocationListener = null; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); final boolean gpsEnabled = mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!gpsEnabled) { // ToDo: GPS 有効 } mLocationListener = new LocationListener() { // http://developer.android.com/reference/android/location/Location.html public void onLocationChanged(Location location) { String[] out = new String[]{String.valueOf(location.getLatitude()), String.valueOf(location.getLongitude()), String.valueOf(location.getAltitude()), String.valueOf(location.getAccuracy()), String.valueOf(location.getSpeed()), String.valueOf(location.getBearing())}; MySQLiteHelper.writeGPS(MainActivity.this, out); String out1 = "\n" + "\n" + "---------- Location --------\n" + "緯度(Latitude)\n\t" + String.valueOf(location.getLatitude()) + " 度\n" + "経度(Longitude)\n\t" + String.valueOf(location.getLongitude()) + " 度\n" + "高度(Altitude)\n\t" + String.valueOf(location.getAltitude()) + " m\n" + "精度(Accuracy)\n\t" + String.valueOf(location.getAccuracy()) + " m\n" + "速度(Speed)\n\t" + String.valueOf(location.getSpeed()) + " m/s\n" + "方角(Bearing)\n\t" + String.valueOf(location.getBearing()) + " 度\n" + // "時刻(Time)\n\t" + String.valueOf(location.getTime()) + " ミリ秒\n" + "\n"; TextView t = (TextView) findViewById(R.id.textView1); t.setText(out1); } //位置情報状態変更を通知する時に呼ばれる public void onStatusChanged(String provider, int status, Bundle extras) { } //位置情報取得有効化を通知する時に呼ばれる public void onProviderEnabled(String provider) { } //位置情報取得無効化を通知する時に呼ばれる public void onProviderDisabled(String provider) { } }; } //開始時に呼ばれる。 protected void onStart() { super.onStart(); mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 1/*mSec*/, 1/*meter*/, mLocationListener); } //一時停止時に呼ばれる。 protected void onStop() { super.onStop(); mLocationManager.removeUpdates(mLocationListener); super.onPause(); } }
MySQLiteHelper.java class MySQLiteHelper extends SQLiteOpenHelper { private static final int DATABASE_VERSION = 2; private static final String DATABASE_NAME = "test.db"; public static final String TABLE_NAME2 = "mytable2";//GPSのためのtable public static final String COLUMN_ID = "_id"; public static final String COLUMN_DATE = "date"; public static final String COLUMN_LATITUDE = "latitude"; public static final String COLUMN_LONGITUDE = "longitude"; public static final String COLUMN_ALTITUDE = "altitude"; public static final String COLUMN_ACCURACY = "accuracy"; public static final String COLUMN_SPEED = "speed"; public static final String COLUMN_BEARING = "bearing"; private static final String MY_CREATE_TABLE2 = "CREATE TABLE " + TABLE_NAME2 + "(" + COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + COLUMN_LATITUDE + "TEXT NOT NULL," + COLUMN_LONGITUDE + " TEXT NOT NULL," + COLUMN_ALTITUDE + "TEXT NOT NULL," + COLUMN_ACCURACY + "TEXT NOT NULL," + COLUMN_SPEED + "TEXT NOT NULL," + COLUMN_BEARING + "TEXT NOT NULL," + COLUMN_DATE + " DEFAULT CURRENT_TIMESTAMP );"; private static final String DROP_TABLE2 = "drop table2 " + TABLE_NAME2; public MySQLiteHelper(Context context) { super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase db) { db.execSQL(MY_CREATE_TABLE2); } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { db.execSQL(DROP_TABLE2); onCreate(db); //tableを削除して再生成 } public static void writeGPS(Context context, String[] out) { SQLiteDatabase db = new MySQLiteHelper(context).getWritableDatabase(); try { ContentValues values = new ContentValues(); values.put(COLUMN_LATITUDE, out[0]); values.put(COLUMN_LONGITUDE, out[1]); values.put(COLUMN_ALTITUDE, out[2]); values.put(COLUMN_ACCURACY, out[3]); values.put(COLUMN_SPEED, out[4]); values.put(COLUMN_BEARING, out[5]); db.insert(TABLE_NAME2, null, values); } finally { db.close(); } } }

###補足情報(言語/FW/ツール等のバージョンなど)
AndoroidStudioで開発しています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

Y.H.

2016/12/22 02:40

「GPSの情報がデータベースのテーブルに入りません。 」GPSの情報以外(例えば何らかの固定値)なら正常に動作するのでしょうか?
guest

回答1

0

ベストアンサー

・out1は正しく取れていますか?
・db.insert(TABLE_NAME2, null, values)の戻り値はどうなっていますか?

投稿2016/12/21 10:11

yona

総合スコア18155

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

退会済みユーザー

退会済みユーザー

2016/12/21 10:56

out1はtextviewに入れて表示できているので取れていると思います。 すいません。勉強不足で戻り値がどうとかわかってないんですが、TABLE_NAME2に値が入るということではないのでしょうか?
yona

2016/12/21 13:44

メソッドの戻り値を出力してください。
退会済みユーザー

退会済みユーザー

2016/12/21 15:50

db.insert(TABLE_NAME2, null, values);をデバックで確認した結果を書きます。 valuse={ContentValuse}bearing=0.0 altitude=0.0 latitude=35.000 speed=0.0 accuracy=50.0 longitude=139.000 順番が関係しているのでしょうか?
yona

2016/12/21 23:37

メソッドの戻り値の意味がわかりますか?
退会済みユーザー

退会済みユーザー

2016/12/22 02:03

すいません、わかってません。 分かりやすく説明してくれると助かります(・_・; 自分では、 MySQLiteHelper.writeGPS(MainActivity.this, out);のところかと思っています
yona

2016/12/22 02:49

Javaの基礎から見直した方がいいですよ。
退会済みユーザー

退会済みユーザー

2016/12/22 02:56

それは自覚していますが、どうしてもこれだけ動かしたいのでヒントをいただけないでしょうか? db.insert(TABLE_NAME2, null, values);の値を返しているところでしょうか?
yona

2016/12/22 03:02

下記の通りに修正してください。 また、これは確認しているだけなので、正しく動かすための修正ではありません。 Log.d("",""+db.insert(TABLE_NAME2, null, values))
退会済みユーザー

退会済みユーザー

2016/12/22 03:56

ご迷惑をおかけしてすいません。 logcatにはこう出ました。 12-22 12:52:18.924 9324-9324/? I/art: Late-enabling -Xcheck:jni 12-22 12:52:18.950 9324-9332/? I/art: Debugger is no longer active 12-22 12:52:18.963 9324-9324/? W/ActivityThread: Application com.example.nishi.aiueo is waiting for the debugger on port 8100... 12-22 12:52:18.967 9324-9324/? I/System.out: Sending WAIT chunk 12-22 12:52:20.147 9324-9332/com.example.nishi.aiueo I/art: Debugger is active 12-22 12:52:20.171 9324-9324/com.example.nishi.aiueo I/System.out: Debugger has connected 12-22 12:52:20.171 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:20.372 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:20.572 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:20.772 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:20.973 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:21.173 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:21.374 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:21.575 9324-9324/com.example.nishi.aiueo I/System.out: waiting for debugger to settle... 12-22 12:52:21.775 9324-9324/com.example.nishi.aiueo I/System.out: debugger has settled (1340) 12-22 12:52:21.810 9324-9324/com.example.nishi.aiueo I/InstantRun: Instant Run Runtime started. Android package is com.example.nishi.aiueo, real application class is null. 12-22 12:52:22.074 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c003f} 12-22 12:52:22.079 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0040} 12-22 12:52:22.082 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0041} 12-22 12:52:22.082 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x801 a=2 r=0x7f08000f} 12-22 12:52:22.082 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x801 a=2 r=0x7f08000e} 12-22 12:52:22.082 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x4001 a=1 r=0x10500cf} 12-22 12:52:22.094 9324-9324/com.example.nishi.aiueo W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 12-22 12:52:22.118 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x801 a=1 r=0x10500d6} 12-22 12:52:22.132 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x1/d=0x7f0900f7 a=2 r=0x7f0900f7} 12-22 12:52:22.132 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0042} 12-22 12:52:22.164 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0050} 12-22 12:52:22.164 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x4001 a=2 r=0x7f080011} 12-22 12:52:22.164 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x1001 a=2 r=0x7f080044} 12-22 12:52:22.164 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x4001 a=2 r=0x7f080011} 12-22 12:52:22.164 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x5/d=0x1001 a=2 r=0x7f080044} 12-22 12:52:22.165 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0033} 12-22 12:52:22.170 9324-9324/com.example.nishi.aiueo W/Resources: Converting to string: TypedValue{t=0x12/d=0x0 a=2 r=0x7f0c0051} 12-22 12:52:22.324 9324-9387/com.example.nishi.aiueo D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 12-22 12:52:22.344 9324-9324/com.example.nishi.aiueo D/Atlas: Validating map...
yona

2016/12/22 04:02

Log.dの第一引数に適当な文字列を入れて、その文字列でフィルタリングしたものだけをコメントに書いてください。
退会済みユーザー

退会済みユーザー

2016/12/22 04:16

ここに表示すると書きました。 12-22 13:09:22.646 17546-17546/com.example.nishi.aiueo E/SQLiteLog: (1) table mytable2 has no column named bearing 12-22 13:09:22.795 17546-17546/com.example.nishi.aiueo E/SQLiteDatabase: Error inserting bearing=0.0 altitude=0.0 latitude=35.6256 speed=0.0 accuracy=19.974 longitude=139.3429099 android.database.sqlite.SQLiteException: table mytable2 has no column named bearing (code 1): , while compiling: INSERT INTO mytable2(bearing,altitude,latitude,speed,accuracy,longitude) VALUES (?,?,?,?,?,?) at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method) at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889) at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500) at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588) at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58) at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31) at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469) at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) at com.example.nishi.aiueo.MySQLiteHelper.writeGPS(MySQLiteHelper.java:64) at com.example.nishi.aiueo.MainActivity$1.onLocationChanged(MainActivity.java:33) at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:281) at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:210) at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:226) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5254) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 12-22 13:09:22.795 17546-17546/com.example.nishi.aiueo D/ここに表示する: -1
yona

2016/12/22 04:34

bearingなんてカラム無いってSQLiteのエラーメッセージ出てるじゃないですか。 クリエイト文が間違っています、カラム名とTEXTの間にスペースがありません。
退会済みユーザー

退会済みユーザー

2016/12/22 06:21

ありがとうがざいます。保存することができました。 本当に感謝しています。もっと勉強します。
yona

2016/12/22 06:44

とにかく自力でデバッグできるようになりましょう。ログを読めないと勉強することもできないです。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問