无法从sqlite android获取数据

时间:2011-11-24 11:32:27

标签: android sqlite

我的数据库名称是AppDB.db,表名是Scrip,其中包含3列_id和符号& company_name,它们是文本。              我在Assets文件夹中复制了数据库。

当应用运行时,会出现第一个检查数据按钮,点击它后,应用程序停止。                 我不知道可能是什么原因。                 请帮忙..

DatabaseSample.class:

            package com.app.DatabaseSample;

            import android.app.Activity;
            import android.content.Intent;
            import android.os.Bundle;
            import android.view.View;
            import android.view.View.OnClickListener;
            import android.widget.Button;

            public class DatabaseSample extends Activity implements OnClickListener {
                /** Called when the activity is first created. */
                @Override
                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    setContentView(R.layout.main);
                    Button btncheck = (Button) findViewById(R.id.btncheck);
                    btncheck.setOnClickListener(this);
                }

                @Override
                public void onClick(View v) {
                    Intent i = new Intent(this, CheckData.class);
                    startActivity(i);
                }
            }

CheckData.class:

            package com.app.DatabaseSample;

            import java.util.ArrayList;
            import java.util.List;

            import android.app.ListActivity;
            import android.os.Bundle;
            import android.util.Log;
            import android.widget.ArrayAdapter;
            import android.widget.TextView;

            public class CheckData extends ListActivity {
                TextView selection;
                DataManipulator dm;
                private static final String TAG = "CheckData";
                List<String[]> list = new ArrayList<String[]>();
                List<String[]> symbol2 = null;
                String[] list1;

                public void onCreate(Bundle savedInstanceState) {
                    super.onCreate(savedInstanceState);
                    try {
                        setContentView(R.layout.check);

                        dm = new DataManipulator(this);
                        // dm.createNewDatabase();
                        dm.open();
                        symbol2 = dm.selectAll();
                        dm.close();

                        list1 = new String[symbol2.size()];
                        int x = 0;
                        String stg;

                        for (String[] symbol : symbol2) {
                            stg = symbol[1];// + " –-> " + symbol[1];
                            list1[x] = stg;
                            x++;
                        }

                        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                                android.R.layout.simple_list_item_1, list1);
                        this.setListAdapter(adapter);
                        selection = (TextView) findViewById(R.id.selection);
                    } catch (Exception e) {
                        Log.e(TAG, "Exception in check class...");
                    }
                }

                // public void onListItemClick(ListView parent, View v, int position, long
                // id) {
                // selection.setText(list1[position]);
                // }
            }

DataManipulator.class:

            package com.app.DatabaseSample;

            import java.util.ArrayList;
            import java.util.List;

            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 DataManipulator extends SQLiteOpenHelper {
                private static final String DATABASE_NAME = "AppDB";
                private static final int DATABASE_VERSION = 1;
                private static final String DATABASE_PATH = "/data/data/com.app.DatabaseSample/databases/";
                private static final String TAG = "DbManipulator";
                static final String TABLE_NAME = "Scrip";
                private Context context;
                SQLiteDatabase db;
                DataManipulator dmDB;

                public DataManipulator(Context context) {
                    super(context, DATABASE_NAME, null, DATABASE_VERSION);
                    this.context = context;
                }

                @Override
                public void onCreate(SQLiteDatabase db) {
                    db.execSQL("CREATE TABLE IF NOT EXISTS Scrip(_id integer primary key autoincrement,"
                            + "symbol text,company_name text);");
                }

                @Override
                public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
                }

                public DataManipulator open() {
                    dmDB = new DataManipulator(context);
                    db = dmDB.getWritableDatabase();
                    return this;
                }

                public void close() {
                    dmDB.close();
                }

                /*
                 * public void createNewDatabase() { InputStream assetsDB = null; try {
                 * assetsDB = context.getAssets().open(DATABASE_NAME); OutputStream dbOut =
                 * new FileOutputStream(DATABASE_PATH + DATABASE_NAME);
                 * 
                 * byte[] buffer = new byte[1024]; int length; while ((length =
                 * assetsDB.read(buffer)) > 0) { dbOut.write(buffer, 0, length); }
                 * 
                 * dbOut.flush(); dbOut.close(); assetsDB.close(); Log.i(TAG,
                 * "New database created..."); } catch (IOException e) { Log.e(TAG,
                 * "Could not create new database..."); e.printStackTrace(); } }
                 */

                public List<String[]> selectAll() {

                    List<String[]> list = new ArrayList<String[]>();
                    // Cursor cursor = db
                    // .query(TABLE_NAME, new String[] {"symbol", "company_name" }, null,
                    // null, null, null, null);
                    try {
                        Cursor cursor = db.rawQuery(
                                "select symbol,company_name from Scrip", new String[] {
                                        "symbol", "company_name" });

                        int x = 0;
                        if (cursor.moveToFirst()) {
                            do {
                                String[] b1 = new String[] {
                                        cursor.getString(cursor.getColumnIndex("symbol")),
                                        cursor.getString(cursor
                                                .getColumnIndex("company_name")) };
                                list.add(b1);
                                Log.e(TAG, "selectAll method Error");
                                x = x + 1;
                            } while (cursor.moveToNext());
                        }
                        if (cursor != null && !cursor.isClosed()) {
                            cursor.close();
                        }
                        cursor.close();

                    } catch (SQLException e) {
                        Log.e("selectAll method Error", e.toString());
                        e.printStackTrace();
                    }
                    return list;
                }
            }

登录CAT

 11-24 17:43:52.535: A/NetworkStats(88): problem reading network stats
11-24 17:43:52.535: A/NetworkStats(88): java.lang.IllegalStateException: problem parsing line: null
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:313)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.server.NetworkManagementService.getNetworkStatsUidDetail(NetworkManagementService.java:1223)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.performPollLocked(NetworkStatsService.java:810)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.updateIfacesLocked(NetworkStatsService.java:721)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.updateIfaces(NetworkStatsService.java:699)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService.access$000(NetworkStatsService.java:128)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.server.net.NetworkStatsService$8.handleMessage(NetworkStatsService.java:1546)
11-24 17:43:52.535: A/NetworkStats(88):     at android.os.Handler.dispatchMessage(Handler.java:95)
11-24 17:43:52.535: A/NetworkStats(88):     at android.os.Looper.loop(Looper.java:137)
11-24 17:43:52.535: A/NetworkStats(88):     at android.os.HandlerThread.run(HandlerThread.java:60)
11-24 17:43:52.535: A/NetworkStats(88): Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)
11-24 17:43:52.535: A/NetworkStats(88):     at libcore.io.IoBridge.open(IoBridge.java:406)
11-24 17:43:52.535: A/NetworkStats(88):     at java.io.FileInputStream.<init>(FileInputStream.java:78)
11-24 17:43:52.535: A/NetworkStats(88):     at java.io.FileReader.<init>(FileReader.java:42)
11-24 17:43:52.535: A/NetworkStats(88):     at com.android.internal.net.NetworkStatsFactory.readNetworkStatsDetail(NetworkStatsFactory.java:272)
11-24 17:43:52.535: A/NetworkStats(88):     ... 9 more
11-24 17:43:52.535: A/NetworkStats(88): Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory)
11-24 17:43:52.535: A/NetworkStats(88):     at libcore.io.Posix.open(Native Method)
11-24 17:43:52.535: A/NetworkStats(88):     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:98)
11-24 17:43:52.535: A/NetworkStats(88):     at libcore.io.IoBridge.open(IoBridge.java:390)
11-24 17:43:52.535: A/NetworkStats(88):     ... 12 more
11-24 17:44:00.085: E/ActivityManager(88): ANR in system
11-24 17:44:00.085: E/ActivityManager(88): Reason: Broadcast of Intent { act=android.intent.action.BOOT_COMPLETED flg=0x10 cmp=android/com.android.server.BootReceiver }
11-24 17:44:00.085: E/ActivityManager(88): Load: 4.62 / 1.5 / 0.53
11-24 17:44:00.085: E/ActivityManager(88): CPU usage from 3420ms to -4642ms ago:
11-24 17:44:00.085: E/ActivityManager(88):   31% 88/system_server: 19% user + 12% kernel / faults: 2796 minor 2 major
11-24 17:44:00.085: E/ActivityManager(88):   19% 34/surfaceflinger: 17% user + 1.5% kernel / faults: 1 minor
11-24 17:44:00.085: E/ActivityManager(88):   14% 349/com.android.launcher: 12% user + 2.5% kernel / faults: 2385 minor
11-24 17:44:00.085: E/ActivityManager(88):   11% 199/com.android.systemui: 9% user + 2.9% kernel / faults: 2953 minor
11-24 17:44:00.085: E/ActivityManager(88):   9.8% 283/android.process.acore: 6.6% user + 3.1% kernel / faults: 1630 minor 1 major
11-24 17:44:00.085: E/ActivityManager(88):   3.5% 64/bootanimation: 3.4% user + 0.1% kernel
11-24 17:44:00.085: E/ActivityManager(88):   1.1% 43/adbd: 0.6% user + 0.5% kernel
11-24 17:44:00.085: E/ActivityManager(88):   1% 226/com.android.phone: 0.3% user + 0.6% kernel / faults: 53 minor
11-24 17:44:00.085: E/ActivityManager(88):   0.1% 35/zygote: 0% user + 0.1% kernel / faults: 75 minor
11-24 17:44:00.085: E/ActivityManager(88):   0.3% 87/logcat: 0% user + 0.3% kernel
11-24 17:44:00.085: E/ActivityManager(88):  +0% 411/com.android.deskclock: 0% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88): 100% TOTAL: 74% user + 25% kernel + 0.1% irq
11-24 17:44:00.085: E/ActivityManager(88): CPU usage from 3713ms to 4380ms later:
11-24 17:44:00.085: E/ActivityManager(88):   53% 199/com.android.systemui: 32% user + 20% kernel / faults: 2625 minor
11-24 17:44:00.085: E/ActivityManager(88):     40% 199/ndroid.systemui: 23% user + 17% kernel
11-24 17:44:00.085: E/ActivityManager(88):     10% 201/GC: 7.8% user + 3.1% kernel
11-24 17:44:00.085: E/ActivityManager(88):   18% 34/surfaceflinger: 15% user + 3% kernel
11-24 17:44:00.085: E/ActivityManager(88):     16% 61/SurfaceFlinger: 16% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88):     1.5% 34/surfaceflinger: 1.5% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88):     1.5% 266/Binder Thread #: 0% user + 1.5% kernel
11-24 17:44:00.085: E/ActivityManager(88):   16% 88/system_server: 10% user + 6% kernel / faults: 63 minor
11-24 17:44:00.085: E/ActivityManager(88):     9% 103/ActivityManager: 7.5% user + 1.5% kernel
11-24 17:44:00.085: E/ActivityManager(88):     4.5% 95/Compiler: 1.5% user + 3% kernel
11-24 17:44:00.085: E/ActivityManager(88):     3% 100/Binder Thread #: 3% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88):     1.5% 106/PackageManager: 0% user + 1.5% kernel
11-24 17:44:00.085: E/ActivityManager(88):     1.5% 281/Binder Thread #: 1.5% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88):   4.5% 64/bootanimation: 4.5% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88):     6% 66/BootAnimation: 6% user + 0% kernel
11-24 17:44:00.085: E/ActivityManager(88):   3% 43/adbd: 1.5% user + 1.5% kernel
11-24 17:44:00.085: E/ActivityManager(88):     1.5% 43/adbd: 0% user + 1.5% kernel
11-24 17:44:00.085: E/ActivityManager(88):   0.4% 87/logcat: 0% user + 0.4% kernel
11-24 17:44:00.085: E/ActivityManager(88):   1.6% 283/android.process.acore: 0% user + 1.6% kernel / faults: 7 minor
11-24 17:44:00.085: E/ActivityManager(88):     1.6% 290/Compiler: 0% user + 1.6% kernel
11-24 17:44:00.085: E/ActivityManager(88): 100% TOTAL: 67% user + 32% kernel

1 个答案:

答案 0 :(得分:0)

发现了这个问题。 DB实际上并没有连接到应用程序。所以它没有得到任何数据。不管怎样,谢谢。