1

我正在使用 sqlite 数据库在 android 中编程。我有一个 DBAdapter 类,它创建我的数据库和表,并有一种将数据插入数据库的方法。在我的 DBActivity 类中,我使用 DBAdapter 类中的方法 insertTitle 来输入数据。运行我的应用程序后,我的数据库中没有任何内容。这段代码实际上是在插入数据吗?任何建议将不胜感激,因为我是使用 android 编程的新手。我没有发布我的 logcat,因为我唯一的错误是打开跟踪文件时出错:没有这样的文件或目录 (2)。我在模拟器上使用它,而不是我的 Droid Bionic 手机。我正在使用 Eclipse SDK 4.2。

DBAdapter 类:

package com.example.database;
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;
import android.widget.EditText;


public class DBAdapter 
{

    EditText dEdit;
    public static final String KEY_ROWID = "_id";
    public static final String KEY_BUSINESS = "business";
    public static final String KEY_ADDRESS = "address";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_HOURS = "hours";
    public static final String KEY_WEB = "website";
    public static final String KEY_TYPE = "type";


    private static final String TAG = "MainActivity";

    private static final String DATABASE_NAME = "LocalDB";
    private static final String DATABASE_TABLE = "Business";
    private static final int DATABASE_VERSION = 6;

    private static final String DATABASE_CREATE =
        "create table Business (_id integer primary key autoincrement, "
        + "business text not null, address text not null, phone text not null,hours text not null,website text not null,type text not null" 
        + ")";

    private final Context context; 

    private DatabaseHelper DBHelper;
    private SQLiteDatabase mDB;

    public DBAdapter(Context ctx) 
    {
        this.context = ctx;
        DBHelper = new DatabaseHelper(context);
    }

    private  class DatabaseHelper extends SQLiteOpenHelper 
    {
        DatabaseHelper(Context context) 
        {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) 
        {
            try{
            mDB=db;
            mDB.execSQL(DATABASE_CREATE);
            } catch(SQLException e){
                e.printStackTrace();
            }
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, 
        int newVersion) 
        {   
            try{
                mDB=db;
            Log.w(TAG, "Upgrading database from version " + oldVersion 
                    + " to "
                    + newVersion + ", which will destroy all old data");
            mDB.execSQL("DROP TABLE IF EXISTS Business");
            onCreate(mDB);
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
    }    

    //---opens the database---
    public DBAdapter open() throws SQLException 
    {
        mDB = DBHelper.getWritableDatabase();
        return this;
    }

    //---closes the database---    
    public void close() 
    {
        DBHelper.close();
    }

    //---insert a title into the database---
    public long insertTitle(String business, String address, String phone, String hours, String website, String type) 
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_BUSINESS, business);
        initialValues.put(KEY_ADDRESS, address);
        initialValues.put(KEY_PHONE, phone);
        initialValues.put(KEY_HOURS, hours);
        initialValues.put(KEY_WEB, hours);
        initialValues.put(KEY_TYPE, type);
        long result= mDB.insert(DATABASE_TABLE, null, initialValues);
        return Log.d("Debug", "insertResult: " +result);
    }

    //---deletes a particular title---
    public boolean deleteTitle(long rowId) 
    {
        return mDB.delete(DATABASE_TABLE, KEY_ROWID + 
                "=" + rowId, null) > 0;
    }

    //---updates a title---
    public boolean updateTitle(long rowId, String business, 
    String address, String phone, String hours, String website, String type) 
    {
        ContentValues args = new ContentValues();
        args.put(KEY_BUSINESS, business);
        args.put(KEY_ADDRESS, address);
        args.put(KEY_PHONE, phone);
        args.put(KEY_HOURS,hours);
        args.put(KEY_WEB,website);
        args.put(KEY_TYPE, type);
        return mDB.update(DATABASE_TABLE, args, 
                         KEY_ROWID + "=" + rowId, null) > 0;
    }


    public Cursor getBusinessInfo(String business) throws SQLException {
        // TODO Auto-generated method stub
          return mDB.rawQuery("SELECT business,address,phone,hours,website,type FROM Business  where business like ?", new String[]{"%"+business+"%"}); 
    }

    public Cursor getBusinessType(String type)throws SQLException {
        // TODO Auto-generated method stub
        return mDB.rawQuery("SELECT business,address,phone,hours,website,type FROM Business  where type like ?", new String[]{"%"+type+"%"}); 
    }
}

数据库使用:

package com.example.database;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;



public class DBUse extends Activity  {
    EditText dEdit;
    DBAdapter data;
    long id;
    public static final String KEY_ROWID = "_id";
    public static final String KEY_BUSINESS = "business";
    public static final String KEY_ADDRESS = "address";
    public static final String KEY_PHONE = "phone";
    public static final String KEY_HOURS = "hours";
    public static final String KEY_WEB = "website";
    public static final String KEY_TYPE = "type";




    @Override
    public void onCreate(Bundle savedInstanceState) {

        // TODO Auto-generated method stub


        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        data = new DBAdapter(this);


        Button buttonName =(Button)findViewById(R.id.buttonName);
        buttonName.setOnClickListener(startName);



        Button buttonType =(Button)findViewById(R.id.buttonType);
        buttonType.setOnClickListener(startType);

        dEdit =(EditText)findViewById(R.id.textView1);

        data.open();

        id =data.insertTitle("BloomingFoods","419 E. Kirkwood Ave. Bloomington, IN", "812-336-5300", "M-Sat:8AM-9PM Sun:9AM-8PM","http://www.bloomingfoods.coop","Grocery");


        id=data.insertTitle("BloomingFoods","316 W. 6th Street Bloomington, IN", "812-333-5300", "M-Sat:7AM-10PM Sun:9AM-9PM","http://www.bloomingfoods.coop","Grocery");

        id=data.insertTitle("BloomingFoods","3220 E. 3rd Street Bloomington, IN", "812-336-5300", "M-Sun:8AM-10PM","http://www.bloomingfoods.coop","Grocery");
        id=data.insertTitle("The Uptown Cafe","102 E. Kirkwood Ave. Bloomington, IN", "812-339-0900", "M-Sat:8AM-9PM Sun:9AM-8PM", "http://www.the-uptown.com/","Cajun-Creole");
        id=data.insertTitle("Crazy Horse","214 W. Kirkwood Ave. Bloomington, IN", "812-336-8877", "M-Sat:11AM-2AM Sun:Noon-Midnight", "http://www.crazyhorseindiana.com/","Restaurant & Live Music");
        id=data.insertTitle("FARMbloomington","108 E. Kirkwood Ave. Bloomington, IN", "812-323-0002", "Tue.-Thurs.:8AM-10PM F:8AM-1AM Sat: 10AM-3AM Sun: 10AM-10PM","http://www.farm-bloomington.com","Grocery");
        id=data.insertTitle("Laughing Planet","322 E. Kirkwood Ave. Bloomington, IN", "812-323-2233", "M-Sun:11AM-9PM","http://www.facebook.com/laughingplanetbtown","Burritos & Salads");
        id=data.insertTitle("Soma Coffeehouse","322 E. Kirkwood Bloomington, IN", "812-331-2770", "M-Sat:7AM-11PM Sun:8AM-11PM","http://www.iheartsoma.com","Coffee House");
        id=data.insertTitle("Upland Brewing Company","350 W. 11th St. Bloomington, IN", "812-336-2337", "M-Thurs:11AM-Midnight F-Sat:11AM-1AM Sun: Noon-Midnight","http://www.uplandataeer.com","Brewery & Restaraunt");
        id=data.insertTitle("Nick's English Hut","423 E. Kirkwood Bloomington, IN", "812-332-4040", "M-Sat:11AM-2AM Sun: Noon-Midnight","http://www.nicksenglishhut.com","Bar & Restaraunt");
        id=data.insertTitle("The Village Deli","409 E. Kirkwood Ave. Bloomington, IN", "812-336-2303", "M-F:7AM-8PM Sat-Sun 8AM-8PM","http://www.villagedeli.biz","Breakfast,Lunch,Dinner");
        id=data.insertTitle("Rachael's Cafe","300 E. 3rd St. Bloomington, IN", "812-330-1882", "M-F:8AM-9PM Sat-Sun 9AM-9PM","http://www.rachaelscafe.com","Coffee House & Live Music");
        id=data.insertTitle("Happy Pig","1604 W. 7th St. Bloomington, IN", "None", "W:Sample Gates Noon-3PM Thurs:Sample Gates Noon-3PM F:Atlas Bar 11:30PM-3:30AM Sat:Courthouse Square 11:30PM-3:30AM","http://www.happypigbloomington.com","BBQ Streetfood");
        id=data.insertTitle("Lennie's Restaurant and Brewpub","1795 E. 10th St. Bloomington, IN", "812-323-2112", "Sun-Th:11AM-11PM F-Sat 11AM-Midnight","http://www.lenniesgourmetpizza.com","Bar & Restaurant");
        id=data.insertTitle("Pizza X","1791 E. 10th St. Bloomington, IN", "812-339-7737", "Sun-Tues:11AM-2AM Wed-Thur: 11AM-3AM F-Sat:11AM-4AM","http://www.pizzaxbloomington.com","Pizza");
        id=data.insertTitle("Pizza X","1610 W. 3rd St. Bloomington, IN", "812-332-2522", "Sun-Wed:4PM-Midnight Thur: 4PM-3AM F-Sat:4PM-4AM","http://www.pizzaxbloomington.com","Pizza");
        id=data.insertTitle("Pizza X","877 S. College Mall Rd. Bloomington, IN", "812-355-5000", "Sun-Wed:4PM-Midnight Thur: 4PM-3AM F-Sat:4PM-4AM","http://www.pizzaxbloomington.com","Pizza");
        id=data.insertTitle("Pizza X","2443 S. Walnut St. Pk. Bloomington, IN", "812-332-8500", "Sun-Wed:4PM-Midnight Thur: 4PM-3AM F-Sat:4PM-4AM","http://www.pizzaxbloomington.com","Pizza");
        id=data.insertTitle("The Atlas Bar","209 S. College Ave.  Bloomington, IN", "812-334-4435", "M-F:5PM-3AM Sat-Sun:7PM-3AM","http://www.atlasballroom.com","Bar");
        id=data.insertTitle("The Bishop","123 S. Walnut St.  Bloomington, IN", "812-333-4700", "Mon-Sat:7PM-3AM","http://www.thebishopbar.com","Bar & Live Music");
        id=data.insertTitle("Oliver Winery","8024 N. State Road 37 Bloomington, IN", "812-876-5800", "Mon-Sat:10AM-6PM Sun: 12PM-6PM Tours:F-Sat Noon-4:30PM Sun:1PM-4:30PM","http://www.oliverwinery.com","Winery");
        id=data.insertTitle("Revolution Bike and Bean","401 E. 10th St. Bloomington, IN", "812-336-0241", "Mon-Fri:10AM-6PM Sat: 10AM-5PM","http://www.revolutionbikeandataean.com","Coffee House & Bike Repair Shop");
        id=data.insertTitle("Mother Hubbard's Cupboard","1010 S. Walnut Bloomington, IN", "812-355-6843", "Mon-Fri:4PM-6PM","http://www.mhcfoodpantry.org","Food Pantry Service & Non-Profit");
        id=data.insertTitle("Patricia's Wellness Arts Cafe and Quilter's Comfort Teas","725 W. Kirkwood Ave. Bloomington, IN", "812-331-0886", "Mon-Sat:11AM-5PM First Friday: 11AM-8PM","http://www.hartrock.net/cafe.htm","Tea House & Holistic and Naturopathic & Reiki & Counseling");
        id=data.insertTitle("One World Catering and Events","2234 W. Industrial Pk. Dr. Bloomington, IN", "812-334-3663", "","http://www.oneworldcateringandevents.com","Catering");
        id=data.insertTitle("Nelson's Furniture Restoration","6573 S. Old State Road 37 Bloomington, IN", "812-824-7769", "M-F:10AM-6PM","http://www.nelsonrestoration.com","Furniture Restoration");
        id=data.insertTitle("NR Hiller Design, Inc","3450 S. Garrison Chapel. Rd. Bloomington, IN", "812-825-5872", "Hours By Appointment","http://www.nrhillerdesign.com","Furniture Design & Furniture Building & Cabinet-Making & Furniture");
        id=data.insertTitle("Showers Inn Bed and Breakfast","430 N. Washington St. Bloomington, IN", "812-334-9000", "Hours By Appointment","http://www.showersinn.com","Bed and Breakfast");
        id=data.insertTitle("Monroe County History Center","202 E. 6th St.  Bloomington, IN", "812-332-2517", "Tues-Sat:10AM-4PM","http://www.monroehistory.org","Museum");
        id=data.insertTitle("WFHB","108 W. 4th St. Bloomington, IN", "812-323-0320", "Hours By Appointment","http://www.wfhb.org","Radio Station & Non-Profit");
        id=data.insertTitle("Brinegar Eye Care","4001 E. 3rd St. Suite 8 Bloomington, IN", "812-339-7995", "Mon-F:9AM-5PM","http://www.brinegareyecare.com","Optometrist");
        id=data.insertTitle("Sole Sensations","414 S. College Ave. Bloomington, IN", "812-331-1962", "Mon:10AM-6PM Tues:10AM-8PM Wed:10AM-6PM Thurs:10AM-8PM Sat:10AM-5PM","http://www.solesensations.com/Soul-Inspirations.html","Orthotics & Shoes");
        id=data.insertTitle("Bloomignton Area Birth Services (BABS)","2458 S. Walnut St. Bloomington, IN", "812-337-8121", "Mon-Fri:10AM-5PM Sat:10AM-2PM" ,"http://www.bloomingtonbirth.org","Holistic and Naturopathic & Non-Profit & Gifts & Toys & Books & Maternity/Nursing Wear & Prenatal and Postnatal Yoga & Childatairth Classes & Breastfeeding Classes and Consultation");
        id=data.insertTitle("Karen Knight, LMHC Counseling Services","115 N. College Ave Suite 214 Bloomington, IN", "812-361-3601", "Mon-Fri:9AM-8PM By Appointment" ,"http://www.karenknight.net","Mental Health & Counseling");
        id=data.insertTitle("Mister Buck's Genuinely Good Pet Food Compnay","319 S. Mitchell Bloomington, IN", "812-384-3853", "24hr Internet Access" ,"http://www.mrbuckspetfood.com","Pet Care");
        id=data.insertTitle("Center for Sustainable Living","323 S. Walnut St. Bloomington, IN", "812-332-8796", "24hr Internet Access" ,"http://www.simplycsl.org","Non-Profit");
        id=data.insertTitle("Friends of Art Bookshop","1201 E. 7th St. (Fine Arts Building, Indiana University) Bloomington, IN", "812-855-7498","M-Thurs:9AM-6PM Sat-Sun:1PM-5PM" ,"http://www.fa.indiana.edu/foart/","Non-Profit & Books & Art Books & Textbooks");
        id=data.insertTitle("Cardinal Stage Company","900 S. Walnut St. Bloomington, IN", "812-323-3020", "24hr Internet Access" ,"http://www.cardinalstage.org","Non-Profit & Theater");
        id=data.insertTitle("Bloomington Playwrights Project","107 W. 9th St. Bloomington, IN", "812-334-1188", "Mon-Fri:9AM-5PM" ,"http://www.newplays.org","Non-Profit");
        id=data.insertTitle("Boxcar Books","408 E. 6th St. Bloomington, IN", "812-339-8710", "Mon-Fri: 11AM-9PM Sat:10AM-9PM Sun:10AM-5PM" ,"http://www.boxcarbooks.org","Non-Profit & Books & Textbooks");
        id=data.insertTitle("Community Foundation of Bloomington and Monroe County,Inc.","101 W. Kirkwood Ave. Suite 321 Bloomington, IN", "812-333-9016", "Mon-Fri:8:30AM-5PM" ,"http://www.cfbmc.org","Non-Profit");
        id=data.insertTitle("Windfall Dancers","1101 N. Dunn St. Bloomington, IN", "812-334-0506", "24hr Internet Access" ,"http://www.windfalldancers.org","Non-Profit & Modern Dance & Dance");
        id=data.insertTitle("The Updraft Supplementary Scholarship Project, Inc. (USSP)","403 E. 6th St. Bloomington, IN", "812-961-3553 Toll Free:877-451-8535", "Mon-Fri:9AM-5PM" ,"http://www.usspkids.org","Non-Profit");
        id=data.insertTitle("Story Insights","P.O. Box 2264. Bloomington, IN 47402", "812-340-0479", "24hrs" ,"http://storyinsights.com","Non-Profit & Story Development & Project Management & Web Design");
        id=data.insertTitle("Middle Way House","P.O. Box 95 Bloomington, IN 47402", "Administration:812-333-7404 Crisis Line:812-336-9063", "24hr Internet Access" ,"http://www.middlewayhouse.org","Non-Profit");
        id=data.insertTitle("Baugh Enterprises, Inc","1611 S Rogers St. Bloomington, IN", "812-334-8189", "Mon-Fri:8:30AM-5PM" ,"http://www.baughenterprises.com","Printing Service & Printwork Design");
        id=data.insertTitle("White Rabbit","118 W. 6th St. Bloomington, IN", "812-339-5020 Fax:812-339-7847", "Mon-Fri:8:30AM-5PM" ,"http://www.whiterabbitcopyservice.com","Printing Service");
        id=data.insertTitle("Mr. Copy","501 E. 10th St. Bloomington, IN", "812-334-2679", "Mon-Fri:9AM-6PM Sat: Noon-5PM" ,"http://www.copysales.com","Printing Service & Graphic Design");
        id=data.insertTitle("Rosenplot Design","P.O Box 1083 Bloomington, IN 47402", "812-822-2077 Fax:812-822-2077", "Mon-Fri:8:30AM-5PM" ,"http://www.rosenplotdesign.com","Graphic Design");
        id=data.insertTitle("Patrick Siney Art Direction and Design","1303 S. Stull Ave. Bloomington, IN", "812-334-0019", "Mon-Fri:8:30AM-5PM" ,"http://www.patricksiney.com","Graphic Design & Web Design");
        id=data.insertTitle("Bloomington Cooking School","115 N. College Ave. Suite 014 Bloomington, IN ", "812-333-7100", "Mon-Fri:8:30AM-5PM" ,"http://www.bloomingtoncookingschool.com","Cooking Class");
        id=data.insertTitle("Bloomington Pedal Power","", "812-325-2777", "Mon-Wed:8AM-5PM Thurs-Sat:8AM-2AM" ,"http://www.bloomingtonpedalpower.com","Delivery Service");
        id=data.insertTitle("Backyard Classrooms LLC","Elletsville,IN", "812-844-5826", "Mon-Sun:8AM-8PM" ,"http://www.backyardclassroms.com","Education & Group Events & Agritourism");
        id=data.insertTitle("Kemp & Associates","403 E. 6th St. Bloomington,IN", "812-961-3533 Fax:812-961-2072 Toll Free:877-451-8535", "By Appointment Mon-Fri:9AM-5PM" ,"http://www.kempandassociates.com","Financial Retirement Planning & Estate Planning & Life and Health Insurance Planning & Family Office Services");
        id=data.insertTitle("ISU The May Agency","1327 N. Walnut St. Bloomington, IN", "812-334-2400", "Mon-Fri:9AM-5PM" ,"http://www.MayAgency.com","Life and Health Insurance Planning");
        id=data.insertTitle("LNoggleDesigns","", "812-320-5865", "Flexible" ,"http://www.lnoggledesigns.com","Illustration & Architectural Design & Creative Consultation & Architecture");
        id=data.insertTitle("Sustainability Dashboard Tools LLC","300 W. Hillside Dr. Bloomington, IN", "812-269-2805", "Mon-Fri:9AM-5PM" ,"http://www.green2sustainable.com","Sustainability Services");
        id=data.insertTitle("PrimeSource Office Products, Inc","5015 N. Capitol Ave. Bloomington, IN", "812-876-1143", "Mon-Fri:8AM-5PM" ,"http://www.primesourceop.com","Office Supply");
        id=data.insertTitle("The Dog Knows Detection","P.O. Box 3122 Bloomington, IN 47401", "812-334-7959", "By Appointment" ,"http://www.thedogknowsdetection.com","Pest Control");
        id=data.insertTitle("Easy Go Lawn Care","5929 W. Koontz Rd. Bloomington, IN", "812-320-2844", "By Appointment" ,"http://locallawncareservice.com","Lawn Care & Gutter Cleaning & Snow Removal");
        id=data.insertTitle("Clutter Wrangler","", "812-334-2400", "By Appointment" ,"http://www.clutterwrangler.com","Professional Organizer");
        id=data.insertTitle("Ann Schertz Photography","P.O. Box 1403 Bloomington, IN 47402", "812-327-3402", "By Appointment" ,"http://www.annschertz.com","Photography");
        id=data.insertTitle("The Ryder Magazine and Film Series","Bloomington, IN", "812-339-2002", "" ,"http://www.theryder.com","Magazine & Film Showings");
        id=data.insertTitle("Bloom Magazine","P.O. Box 1204 Bloomington, IN 47402", "812-323-8959", "" ,"http://www.magbloom.com","Magazine");
        id=data.insertTitle("Gallagher Properties","107 N. College Ave. Bloomington, IN", "812-333-5252", "By Appointment" ,"http://www.homefinder.org/findrealtor/findRealtor-AgentSearch.asp?mode=Office_Clicked&BrokerID=175","Housing & Property Management & Commercial Real Estate");
        id=data.insertTitle("Jeanne Walters Real Estate","107 E. 6th St. Bloomington, IN", "866-877-6677", "By Appointment" ,"http://www.jeannewalters.com","Housing");
        id=data.insertTitle("Olympus Properties","2620 N. Walnut St. Bloomington, IN", "812-334-8200", "Mon-Fri:8:30AM-4:30PM" ,"http://www.olympusproperties.com","Housing & Property Management");
        id=data.insertTitle("Blue Studio Gallery","116.5 S. College Ave. Upstairs#10 Bloomington, IN", "812-361-7504", "Mon/Wed/Thurs:Noon-6PM F-Sat:3PM-6PM By Appointment" ,"http://www.bluestudiogallery.com","Art & Gallery");
        id=data.insertTitle("A.Z. Vintage","236 N. Morton St. Bloomington, IN", "812-333-1960", "Mon:By Appointment Tues-Sat:10AM-7PM Sun:Noon-5PM" ,"http://www.azvintagebloomington.com","Art & Gifts & Shoes & Furniture & Handmade Art & Jewelry & Clothing & Vintage");
        id=data.insertTitle("Sweany Artworks-Wandering Turtle On-Line","P.O. Box 1476 Bloomington, IN 47402", "812-219-2992", "By Appointment" ,"http://www.wanderingturtle.com","Art & Gifts & Online Art Gallery");
        id=data.insertTitle("Paper Crane Gallery and Studio","116 S. Grant St. Bloomington, IN", "", "Wed-Fri:2PM-8PM Sat:10AM-4PM Sun:Noon-5PM" ,"http://www.papercranegallery.org","Art & Gifts & Illustration");



    }



//displays the data

    public void DisplayTitle(Cursor c)  {
        Toast.makeText(this,
        "Name: " + c.getString(1)+"\n"+
        "Address:" + c.getString(2)+"\n"+
        "Phone:" + c.getString(3)+"\n"+
        "Hours:" + c.getString(4)+"\n"+
        "Website"+ c.getShort(5)+
        "Type" + c.getShort(6),
        Toast.LENGTH_LONG).show();


    }

    //Informs the user what is being searched
    private OnClickListener startName=new OnClickListener(){

        public void onClick (View v) {

                Cursor cur=data.getBusinessInfo(dEdit.getText().toString());
                String result="";
                int iRow=cur.getColumnIndex(KEY_ROWID);
                int iName=cur.getColumnIndex(KEY_BUSINESS);
                int iAddress=cur.getColumnIndex(KEY_ADDRESS);
                int iPhone=cur.getColumnIndex(KEY_PHONE);
                int iHours=cur.getColumnIndex(KEY_HOURS);
                int iWebsite=cur.getColumnIndex(KEY_WEB);
                int iType=cur.getColumnIndex(KEY_TYPE);
                Toast.makeText(DBUse.this, "Searching by Name", Toast.LENGTH_LONG).show();
            //  log.d("result",DBUse.getString(0));
                for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()){
                    result=result +cur.getString(iRow) + " "
                            +cur.getString(iName) + " "
                            +cur.getString(iAddress) + " "
                            +cur.getString(iPhone) + " "
                            +cur.getString(iHours) + " "
                            +cur.getString(iWebsite) + " "
                            +cur.getString(iType) + " " + "\n";
                    Toast.makeText(DBUse.this, result, Toast.LENGTH_LONG).show();
            };

                return;
            }

        };


//Informs the user what is being searched
    private OnClickListener startType=new OnClickListener(){

            public void onClick (View v){

                    // TODO Auto-generated method stub
                    Cursor cur=data.getBusinessType(dEdit.getText().toString());
                    String result="";
                    int iRow=cur.getColumnIndex(KEY_ROWID);
                    int iName=cur.getColumnIndex(KEY_BUSINESS);
                    int iAddress=cur.getColumnIndex(KEY_ADDRESS);
                    int iPhone=cur.getColumnIndex(KEY_PHONE);
                    int iHours=cur.getColumnIndex(KEY_HOURS);
                    int iWebsite=cur.getColumnIndex(KEY_WEB);
                    int iType=cur.getColumnIndex(KEY_TYPE);
                Toast.makeText(DBUse.this, "Searching by Type", Toast.LENGTH_LONG).show();
                //  log.d("result",DBUse.getString(0));
                    for(cur.moveToFirst(); !cur.isAfterLast(); cur.moveToNext()){
                        result=result +cur.getString(iRow) + " "
                                +cur.getString(iName) + " "
                                +cur.getString(iAddress) + " "
                                +cur.getString(iPhone) + " "
                                +cur.getString(iHours) + " "
                                +cur.getString(iWebsite) + " "
                                +cur.getString(iType) + " " + "\n";
                //  Toast.makeText(DBUse.this, result, Toast.LENGTH_LONG).show();
                };
                return;

                }

        };


    };

显现:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.database"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


    <application
        android:allowBackup="true"
        android:debuggable="true"
        android:icon="@drawable/icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.database.DBUse"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

        </activity>

    </application>

</manifest>

日志猫:

04-29 04:15:41.614: E/Trace(797): error opening trace file: No such file or directory (2)
04-29 04:15:42.573: D/dalvikvm(797): GC_FOR_ALLOC freed 91K, 2% free 10842K/11011K, paused 54ms, total 56ms
04-29 04:15:42.583: I/dalvikvm-heap(797): Grow heap (frag case) to 11.204MB for 614416-byte allocation
04-29 04:15:42.673: D/dalvikvm(797): GC_CONCURRENT freed 1K, 2% free 11441K/11655K, paused 44ms+15ms, total 87ms
04-29 04:15:42.734: D/dalvikvm(797): GC_FOR_ALLOC freed 0K, 2% free 11441K/11655K, paused 26ms, total 26ms
04-29 04:15:42.744: I/dalvikvm-heap(797): Grow heap (frag case) to 12.245MB for 1093136-byte allocation
04-29 04:15:42.834: D/dalvikvm(797): GC_CONCURRENT freed 0K, 2% free 12508K/12743K, paused 36ms+15ms, total 92ms

04-29 04:15:44.836: D/gralloc_goldfish(797): Emulator without GPU emulation detected.
04-29 04:15:50.544: E/CursorWindow(797): Failed to read row 0, column -1 from a CursorWindow which has 4 rows, 6 columns.
04-29 04:15:50.544: D/AndroidRuntime(797): Shutting down VM
04-29 04:15:50.544: W/dalvikvm(797): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
04-29 04:15:50.579: E/AndroidRuntime(797): FATAL EXCEPTION: main
04-29 04:15:50.579: E/AndroidRuntime(797): java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.database.CursorWindow.nativeGetString(Native Method)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.database.CursorWindow.getString(CursorWindow.java:434)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
04-29 04:15:50.579: E/AndroidRuntime(797):  at com.example.database.DBUse$1.onClick(DBUse.java:198)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.view.View.performClick(View.java:4084)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.view.View$PerformClick.run(View.java:16966)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.os.Handler.handleCallback(Handler.java:615)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:137)
04-29 04:15:50.579: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-29 04:15:50.579: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
04-29 04:15:50.579: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:511)
04-29 04:15:50.579: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-29 04:15:50.579: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-29 04:15:50.579: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)
04-29 04:15:52.774: I/Process(797): Sending signal. PID: 797 SIG: 9
4

2 回答 2

1

您可以尝试下载:http ://sqlitebrowser.sourceforge.net/这是一个查看 SQLite 数据库的程序。在 Eclipse 中,打开文件浏览器并转到 /data/data/ 并找到您的包名称和数据库文件。将其保存到您的计算机并使用此程序查看它以检查是否插入了数据(或者错误是否来自其他原因,例如您在应用程序中获取数据)。

于 2013-04-29T02:48:39.260 回答
0

运行应用程序转到 DDMS--> 文件资源管理器--> 数据--> 数据--> 您的应用程序名称,如 com.example.demoApplication 展开时。您应该看到文件夹一个是数据库,一个是 lib。请查看LocalDB(即您的数据库名称)是否存在。如果它不存在,那么您的数据库没有被创建。在这种情况下,我建议使用sqlite浏览器创建数据库,然后将其导入到同一个数据库文件夹下你的申请。

于 2013-04-29T05:49:05.433 回答