English 中文(简体)
无法从 s和roid取数据
原标题:not able to fetch data from sqlite android

My database name is AppDB.db and table name is Scrip which contains 3 columns _id and symbol&company_name which are text. I have copied database in Assets folder.

When app runs, first check data button appears, on clicking it, app stops. I don t know what might be the reason. Please help..

<>Database Sample>:

            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);
                }
            }

www.un.org/Depts/DGACM/index_french.htm

            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:

            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;
                }
            }

<<>LOG 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
最佳回答

found the problem. DB doesn t actually got connected to app. So it was not getting any data. Thanks anyways.

问题回答

暂无回答




相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签