概要
状態:試行錯誤中
閲覧数:1,594
投稿日:2013-08-03
更新日:2013-08-07
項目に入力したデータをINSERT後、表示
修正内容
・どう表示したら良いか不明なため、とりあえずテキストビュー「dbtest2」を新たに作成
▼/src/android/style/householdaccount/MainActivity.java
・onCreateメソッド内の最後
/* メソッド処理 */
// SQLiteDatabase db=database();
SubOpenHelper helper = new SubOpenHelper(getApplicationContext(),"test.db4",1);
SQLiteDatabase db;
db = helper.getWritableDatabase();
line();
// createTable();
// createMonthTable();
// insert();
db.execSQL("insert into Date_Table (Item,Utiwake,Kingaku) values ('"+d1+"', '"+d2+"',"+d3+");");
// cursor();
dbtest2.setText("");
//dbtest2の初期化
// カーソルの設定
// String[] cols = {"Koumoku","Utiwake","Kingaku"};
String[] cols = {"Item","Utiwake","Kingaku"};
Cursor c = db.query("Date_Table",cols,null, null, null, null, null,null);
//カーソルのリストを作る。1:テーブル名、2:取得する列名(カラム等)の配列、
//3&4:取得するレコードの条件、5:GROUP BY条件、6:「HAVING」条件、
//7:「ORDER BY」条件、8:「limit」条件
boolean isEof = c.moveToFirst();
//カーソルを先頭に移動
while (isEof) {
//while文。カーソルが最後に行くまで繰り返す。
dbtest2.append(c.getString(0));
dbtest2.append(c.getString(1));
dbtest2.append(String.valueOf(c.getInt(2)));
//getString(0)メソッドで、カーソルの一行目を追加。2,3も同じ。
isEof = c.moveToNext();}
//次のリストにカーソルを移す。
c.close();
//終わったら閉じる。これがないとエラーとなる。データベースも。
db.close();
}
スクロールタグ書くとエラー。なんで?
概要
・スクロールタグ書くとエラー
・原因不明
・しょうがないので、ネットで調べた内容へ変更
▼/res/layout/activity_main.xml
<ScrollView
android:id="@+id/ScrollView"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="400px">
<TextView
android:id="@+id/dbtest2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10.0px"
android:textColor="@color/yellow"
/>
</LinearLayout>
</ScrollView>
項目を、リンク先画面のように並べるには? リスト必須?
概要
・項目が、リンク先画面のように表示されない
理由
・手順が省略されているから
▼/src/android/style/householdaccount/MainActivity.java
// dbtest2.append(c.getString(0));
// dbtest2.append(c.getString(1));
// dbtest2.append(String.valueOf(c.getInt(2)));
dbtest2.append("\n"+c.getString(0)+"\n");
dbtest2.append(" "+c.getString(1)+":"+String.valueOf(c.getInt(2))+"円"+"\n");
配列キー違い
配列キーが、掲載コードと異なる理由は?
・現状コード「0」→「2」なのに、掲載コードは「1」→「3」
掲載コードに合わせ、配列キー「1」→「3」へ変更すると、エラー
08-04 01:19:14.595: E/CursorWindow(291): Bad request for field slot 0,3. numRows = 11, numColumns = 3
理由
・(最終コードから判断するに、)配列構成が違うため
▼/src/android/style/householdaccount/MainActivity.java
・最終コード
String[] cols = {"_id","Item","Utiwake","Kingaku"};
・現状コード
// String[] cols = {"Koumoku","Utiwake","Kingaku"};
String[] cols = {"Item","Utiwake","Kingaku"};
Order by
第7引数
Order by句
・修正前
Cursor c = db.query("Date_Table",cols,null, null, null, null, null,null);
・修正後
Cursor c = db.query("Date_Table",cols,null, null, null, null, "Utiwake desc",null);
Order by句されているように見えない
・カラム名(データベースの行ごとの項目名)降順(desc)指定しているから
・"Item desc"へ変更すると、「Order by句」反映を確認できる
nullが挿入される理由は?
null
NOT NULL制約カラムにnullが挿入される理由は?
・理由不明
現象
・「not null」制約しているはずの「Item」カラムに、nullが挿入されてしまう
▼/src/android/style/householdaccount/SubOpenHelper.java
sql += ",Item text not null";
具体的には
・起動する度にnullが挿入されてしまう
対処療法
・項目がnullじゃなかったら、という条件分岐を追記
▼/src/android/style/householdaccount/MainActivity.java
if(d1 != null){//項目がnullじゃなかったら
db.execSQL("insert into Date_Table (Item,Utiwake,Kingaku) values ('"+d1+"', '"+d2+"',"+d3+");");
}
コマンド履歴
値のデータ型を取得するためにtypeof関数を使いデータ取得
sqlite> select typeof(カラム1) ,typeof(カラム2) ,typeof(カラム3) from テーブル名;
sqlite> select Item, typeof(Item), Utiwake, typeof(Utiwake) , Kingaku, typeof(Kingaku) from Date_Table;
select Item, typeof(Item), Utiwake, typeof(Utiwake) , Kingaku, typeof(Kingaku) from Date_Table;
null|text|null|text||null
null|text|null|text||null
譛・|text|繧キ繝」繝лtext|200|text
スキーマ確認
sqlite> .schema テーブル名
sqlite> .schema Date_Table
.schema Date_Table
CREATE TABLE Date_Table (_id integer primary key autoincrement,Item text not null,Utiwake text,Kingaku text);
sqlite>
C:\Users\Administrator>ADB
Android Debug Bridge version 1.0.31
-d - directs command to the only connected USB device
returns an error if more than one USB device is present.
-e - directs command to the only running emulator.
returns an error if more than one emulator is running.
-s <specific device> - directs command to the device or emulator with the given
serial number or qualifier. Overrides ANDROID_SERIAL
environment variable.
-p <product name or path> - simple product name like 'sooner', or
a relative/absolute path to a product
out directory like 'out/target/product/sooner'.
If -p is not specified, the ANDROID_PRODUCT_OUT
environment variable is used, which must
be an absolute path.
devices [-l] - list all connected devices
('-l' will also list device qualifiers)
connect <host>[:<port>] - connect to a device via TCP/IP
Port 5555 is used by default if no port number is specified.
disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.
Port 5555 is used by default if no port number is specified.
Using this command with no additional arguments
will disconnect from all connected TCP/IP devices.
device commands:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> [<local>] - copy file/dir from device
adb sync [ <directory> ] - copy host->device only if changed
(-l means list but don't copy)
(see 'adb help all')
adb shell - run remote shell interactively
adb shell <command> - run remote shell command
adb emu <command> - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
adb jdwp - list PIDs of processes hosting a JDWP transport
adb install [-l] [-r] [-s] [--algo <algorithm name> --key <hex-encoded key> --iv <hex-encoded iv>] <file>
- push this package file to the device and install it
('-l' means forward-lock the app)
('-r' means reinstall the app, keeping its data)
('-s' means install on SD card instead of internal storage)
('--algo', '--key', and '--iv' mean the file is encrypted already)
adb uninstall [-k] <package> - remove this app package from the device
('-k' means keep the data and cache directories)
adb bugreport - return all information from the device
that should be included in a bug report.
adb backup [-f <file>] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]
- write an archive of the device's data to <file>.
If no -f option is supplied then the data is written
to "backup.ab" in the current directory.
(-apk|-noapk enable/disable backup of the .apks themselves
in the archive; the default is noapk.)
(-shared|-noshared enable/disable backup of the device's
shared storage / SD card contents; the default is noshared.)
(-all means to back up all installed applications)
(-system|-nosystem toggles whether -all automatically includes
system applications; the default is to include system apps)
(<packages...> is the list of applications to be backed up. If
the -all or -shared flags are passed, then the package
list is optional. Applications explicitly given on the
command line will be included even if -nosystem would
ordinarily cause them to be omitted.)
adb restore <file> - restore device contents from the <file> backup archive
adb help - show this help message
adb version - show version num
scripting:
adb wait-for-device - block until device is online
adb start-server - ensure that there is a server running
adb kill-server - kill the server if it is running
adb get-state - prints: offline | bootloader | device
adb get-serialno - prints: <serial-number>
adb get-devpath - prints: <device-path>
adb status-window - continuously print device status for a specified device
adb remount - remounts the /system partition on the device read-write
adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
adb reboot-bootloader - reboots the device into the bootloader
adb root - restarts the adbd daemon with root permissions
adb usb - restarts the adbd daemon listening on USB
adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port
networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
[parameters] - Eg. defaultroute debug dump local notty usepeerdns
adb sync notes: adb sync [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, both /system and /data partitions will be updated.
- If it is "system" or "data", only the corresponding partition
is updated.
environmental variables:
ADB_TRACE - Print debug information. A comma separated list of the following values
1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.
ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.
C:\Users\Administrator>adb
Android Debug Bridge version 1.0.31
-d - directs command to the only connected USB device
returns an error if more than one USB device is present.
-e - directs command to the only running emulator.
returns an error if more than one emulator is running.
-s <specific device> - directs command to the device or emulator with the given
serial number or qualifier. Overrides ANDROID_SERIAL
environment variable.
-p <product name or path> - simple product name like 'sooner', or
a relative/absolute path to a product
out directory like 'out/target/product/sooner'.
If -p is not specified, the ANDROID_PRODUCT_OUT
environment variable is used, which must
be an absolute path.
devices [-l] - list all connected devices
('-l' will also list device qualifiers)
connect <host>[:<port>] - connect to a device via TCP/IP
Port 5555 is used by default if no port number is specified.
disconnect [<host>[:<port>]] - disconnect from a TCP/IP device.
Port 5555 is used by default if no port number is specified.
Using this command with no additional arguments
will disconnect from all connected TCP/IP devices.
device commands:
adb push <local> <remote> - copy file/dir to device
adb pull <remote> [<local>] - copy file/dir from device
adb sync [ <directory> ] - copy host->device only if changed
(-l means list but don't copy)
(see 'adb help all')
adb shell - run remote shell interactively
adb shell <command> - run remote shell command
adb emu <command> - run emulator console command
adb logcat [ <filter-spec> ] - View device log
adb forward <local> <remote> - forward socket connections
forward specs are one of:
tcp:<port>
localabstract:<unix domain socket name>
localreserved:<unix domain socket name>
localfilesystem:<unix domain socket name>
dev:<character device name>
jdwp:<process pid> (remote only)
adb jdwp - list PIDs of processes hosting a JDWP transport
adb install [-l] [-r] [-s] [--algo <algorithm name> --key <hex-encoded key> --iv <hex-encoded iv>] <file>
- push this package file to the device and install it
('-l' means forward-lock the app)
('-r' means reinstall the app, keeping its data)
('-s' means install on SD card instead of internal storage)
('--algo', '--key', and '--iv' mean the file is encrypted already)
adb uninstall [-k] <package> - remove this app package from the device
('-k' means keep the data and cache directories)
adb bugreport - return all information from the device
that should be included in a bug report.
adb backup [-f <file>] [-apk|-noapk] [-shared|-noshared] [-all] [-system|-nosystem] [<packages...>]
- write an archive of the device's data to <file>.
If no -f option is supplied then the data is written
to "backup.ab" in the current directory.
(-apk|-noapk enable/disable backup of the .apks themselves
in the archive; the default is noapk.)
(-shared|-noshared enable/disable backup of the device's
shared storage / SD card contents; the default is noshared.)
(-all means to back up all installed applications)
(-system|-nosystem toggles whether -all automatically includes
system applications; the default is to include system apps)
(<packages...> is the list of applications to be backed up. If
the -all or -shared flags are passed, then the package
list is optional. Applications explicitly given on the
command line will be included even if -nosystem would
ordinarily cause them to be omitted.)
adb restore <file> - restore device contents from the <file> backup archive
adb help - show this help message
adb version - show version num
scripting:
adb wait-for-device - block until device is online
adb start-server - ensure that there is a server running
adb kill-server - kill the server if it is running
adb get-state - prints: offline | bootloader | device
adb get-serialno - prints: <serial-number>
adb get-devpath - prints: <device-path>
adb status-window - continuously print device status for a specified device
adb remount - remounts the /system partition on the device read-write
adb reboot [bootloader|recovery] - reboots the device, optionally into the bootloader or recovery program
adb reboot-bootloader - reboots the device into the bootloader
adb root - restarts the adbd daemon with root permissions
adb usb - restarts the adbd daemon listening on USB
adb tcpip <port> - restarts the adbd daemon listening on TCP on the specified port
networking:
adb ppp <tty> [parameters] - Run PPP over USB.
Note: you should not automatically start a PPP connection.
<tty> refers to the tty for PPP stream. Eg. dev:/dev/omap_csmi_tty1
[parameters] - Eg. defaultroute debug dump local notty usepeerdns
adb sync notes: adb sync [ <directory> ]
<localdir> can be interpreted in several ways:
- If <directory> is not specified, both /system and /data partitions will be updated.
- If it is "system" or "data", only the corresponding partition
is updated.
environmental variables:
ADB_TRACE - Print debug information. A comma separated list of the following values
1 or all, adb, sockets, packets, rwx, usb, sync, sysdeps, transport, jdwp
ANDROID_SERIAL - The serial number to connect to. -s takes priority over this if given.
ANDROID_LOG_TAGS - When used with the logcat option, only these debug tags are printed.
C:\Users\Administrator>adb shell
# cd data/data/android.style.householdaccount/databases
cd data/data/android.style.householdaccount/databases
# ls
ls
test.db
test.db2
test.db3
test.db4
test.db5
# sqlite3 test.db5
sqlite3 test.db5
SQLite version 3.6.22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .table
.table
Date_Table android_metadata
sqlite> select * from Date_Table;
select * from Date_Table;
1|null|null|
2|null|null|
3|譛・|繧キ繝」繝л200
疑問
1.NOT NULL制約カラムにnullが挿入される理由
2.Item.javaではなく、なぜインテントでMainActivity.javaへ移してから、INSERTするのか?
NOT NULL制約カラムにNULLが格納される - その他(データベース) - 教えて!goo
ソート
ソート
▼/src/android/style/householdaccount/MainActivity.java
1.変数sortに空白を格納
2.「変数」sortと、「項目名」c.getString(1)を 比較
3.一致していなければ、「内訳」「金額」とともに、新たな変数に格納
4.一致していれば、「内訳」「金額」だけを、新たな変数に格納
5.変数sortに項目名を格納
6.次のリスト(行)にカーソルを移す
Cursor c = db.query("Date_Table",cols,null, null, null, null, "Utiwake desc",null);
boolean isEof = c.moveToFirst();//カーソルを先頭に移動
String sort = "";//変数sortに空白を格納
while (isEof) {//while文。カーソルが最後に行くまで繰り返す
if(!(sort.equals(c.getString(0)))){//equals()メソッド … 等価。! … 否定。「変数」sortと「項目名」c.getString(1)が一致していなければ
dbtest2.append("\n"+c.getString(0)+"\n");}
dbtest2.append(" "+c.getString(1)+":"+String.valueOf(c.getInt(2))+"円"+"\n");
sort=c.getString(0);//変数sortに項目名を格納
isEof = c.moveToNext();//次のリスト(行)にカーソルを移す
}
c.close();//終わったら閉じる。これがないとエラーとなる
db.close();//終わったら閉じる。データベースも
ソート修正
不具合対応
・いつの間にかソート出来なくなっていた
・理由不明
▼/src/android/style/householdaccount/MainActivity.java
・onCreateメソッド
// Cursor c = db.query("Date_Table",cols,null, null, null, null, "Utiwake desc",null);
Cursor c = db.query("Date_Table",cols,null, null, null, null, "Item desc",null);