1

我不明白为什么没有这样的表,即使我已经通过编码创建了它。任何人都知道为什么并且可以帮助解决这个问题吗?

07-11 13:41:59.335:I/Database(288):sqlite 返回:错误代码 = 1,msg = 没有这样的表:personalInfo
07-11 13:41:59.345:E/Database(288):插入不喜欢时出错=瑜伽类型=婚礼日期=15/10/2017 喜欢=虾名=Joanne Liew

package main.page;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

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.os.Bundle;
import android.provider.BaseColumns;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;

public class InfoDBAdapter
{
    public static final String  KEY_ROWID        = "_id";
    public static final String  KEY_NAME         = "name";
    public static final String  KEY_DATE         = "date";
    public static final String KEY_TYPE          = "type";
    public static final String KEY_LIKES         = "likes";
    public static final String KEY_DISLIKES      = "dislikes";
    private static final String TAG              = "InfoDBAdapter";

    private static final String DATABASE_NAME    = "AnniversaryDB";
    private static final String DATABASE_TABLE   = "personalInfo";
    private static final int    DATABASE_VERSION    = 2;

    private static final String DATABASE_CREATE  = "create table personalInfo (_id integer primary key autoincrement, "
                                                         + "name text not null, date text not null, type text not null, likes text not null, dislikes text not null);";

    private final Context       context;

    private DatabaseHelper      DBHelper;
    private SQLiteDatabase      db;

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

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

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

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
        {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS info");
            onCreate(db);
        }// end onUpgrade()

    }// end DatabaseHelper

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

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

    // ---insert a contact into the database---
    public long insertContact(String name, String date, String type, String likes, String dislikes)
    {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_NAME, name);
        initialValues.put(KEY_DATE, date);
        initialValues.put(KEY_TYPE, type);
        initialValues.put(KEY_LIKES, likes);
        initialValues.put(KEY_DISLIKES, dislikes);
        return db.insert(DATABASE_TABLE, null, initialValues);
    }// end insertContact()

    // ---deletes a particular contact---
    public boolean deleteContact(long rowId)
    {
        return db.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }// end deleteContact()

    // ---retrieves all the contacts---
    public Cursor getAllContacts()
    {
        return db.query(DATABASE_TABLE, new String[] 
                { KEY_ROWID, KEY_NAME, KEY_DATE, KEY_TYPE, KEY_LIKES, KEY_DISLIKES }, null, null, null, null, null);
    }// end getAllContacts()

    // ---retrieves a particular contact---
    public Cursor getContact(long rowId) throws SQLException
    {
        Cursor mCursor = db.query(true, DATABASE_TABLE, new String[] {
                KEY_ROWID, KEY_NAME, KEY_DATE, KEY_TYPE, KEY_LIKES, KEY_DISLIKES }, KEY_ROWID + "=" + rowId,
                null, null, null, null, null);
        if (mCursor != null)
        {
            mCursor.moveToFirst();
        }
        return mCursor;
    }// end getContact()

    // ---updates a contact---
    public boolean updateContact(long rowId, String name, String date, String type, String likes, String dislikes)
    {
        ContentValues args = new ContentValues();
        args.put(KEY_NAME, name);
        args.put(KEY_DATE, date);
        args.put(KEY_TYPE, type);
        args.put(KEY_LIKES, likes);
        args.put(KEY_DISLIKES, dislikes);
        return db.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }// end updateContact()

}//end DBAdapter    







package main.page;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;


import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class PersonalInfo extends Activity
{
    Button btnSave, btnBack;
    EditText txtDate, txtType, txtLikes, txtDislikes;

    Spinner nameSpinner;
    ArrayAdapter<String>adapter;
    String names[] = {"Kanak Priya", "Joanne Liew", "Michelle Lam", "Natasha Akhbar", "Joan Tang", "Teo Kin Hua", "David Yeo"};

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.info);

        /*try{
        String destPath = "/data/data" + getPackageName() +
                "/databases/AnniversaryDB";
        File f = new File(destPath);
        if(!f.exists())
        {
            CopyDB(getBaseContext().getAssets().open("AnniversaryDB"),
                    new FileOutputStream(destPath));
        }
    }catch (FileNotFoundException e)
    {
        e.printStackTrace();
    }catch (IOException e)
    {
        e.printStackTrace();
    }*/

        InfoDBAdapter dbInfo = new InfoDBAdapter(this);

        dbInfo.open();
        long id = dbInfo.insertContact("Kanak Priya", "24/6/1990", "Birthday", "Painting", "Bungee jumping");
        id = dbInfo.insertContact("Joanne Liew", "15/8/1990", "Birthday", "Skating", "Yoga");
        dbInfo.close();

        /*//---get all contacts--
        dbInfo.open();
        Cursor c = dbInfo.getAllContacts();
        if (c.moveToFirst())
        {
            do
            {
                DisplayContact(c);
            } while (c.moveToNext());
        }
        dbInfo.close();*/


        Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
         adapter = new ArrayAdapter<String>(this,
                 android.R.layout.simple_spinner_item, names);
         nameSpinner.setAdapter(adapter);



        Button btnSave = (Button) findViewById(R.id.btnSave);
        btnSave.setOnClickListener(new View.OnClickListener()
    {

        @Override
        public void onClick(View v)
        {
            Spinner nameSpinner = (Spinner) findViewById(R.id.nameSpinner);
            EditText txtDate = (EditText) findViewById(R.id.txtDate);
            EditText txtType = (EditText) findViewById(R.id.txtType);
            EditText txtLikes = (EditText) findViewById(R.id.txtLikes);
            EditText txtDislikes = (EditText) findViewById(R.id.txtDislikes);

            Toast.makeText(getBaseContext(),
                    "Your info is saved successfully!",
                    Toast.LENGTH_SHORT).show();

        }
    });

    Button btnBack = (Button) findViewById(R.id.btnMain);
    btnBack.setOnClickListener(new View.OnClickListener()
        {

            @Override
            public void onClick(View arg0)
            {
                finish();

            }
        });

    }// end onCreate()

    /*public void CopyDB(InputStream inputStream,
    OutputStream outputStream)
    throws IOException 
{
        //---copy 1K bytes at a time---
        byte[] buffer = new byte[1024];
        int length;
        while ((length = inputStream.read(buffer)) > 0) 
        {
            outputStream.write(buffer, 0, length);
        }
        inputStream.close();
        outputStream.close();
}*/

    /*public void DisplayContact(Cursor c)
    {
        Toast.makeText(
                this,
                "id: " + c.getString(0) + "\n" + "Name: " + c.getString(1)
                        + "\n" + "Date:  " + c.getString(2) + "\n" + "Type: " + c.getString(3) + "\n" + "Likes: " + c.getString(4)
                       + "\n" + "Dislikes: " + c.getString(5),  Toast.LENGTH_LONG)
                .show();
    }*/

}



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    style="@style/InfoStyle"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/fName"
        style="@style/InfoStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Full Name"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner
        android:id="@+id/nameSpinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        >

        <requestFocus />
    </Spinner>

    <TextView
        android:id="@+id/date"
        style="@style/InfoStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Date"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >


        <EditText
            android:id="@+id/txtDate"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:ems="10"
            android:inputType="date" />



    </RelativeLayout>

    <TextView
        android:id="@+id/type"
        style="@style/InfoStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Type"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <EditText
        android:id="@+id/txtType"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" />

    <TextView
        android:id="@+id/bow"
        style="@style/InfoStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="birthday or wedding"
        android:textAppearance="?android:attr/textAppearanceSmall" />

    <TextView
        android:id="@+id/likes"
        style="@style/InfoStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Likes"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <AutoCompleteTextView
        android:id="@+id/txtLikes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="top" />

    <TextView
        android:id="@+id/dislikes"
        style="@style/InfoStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dislikes"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <AutoCompleteTextView
        android:id="@+id/txtDislikes"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:gravity="top" />



    <Button
        android:id="@+id/btnSave"
        style="@style/main_menu"
        android:layout_width="109dp"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:background="@drawable/red_btn_info"
        android:text="Save" />

    <Button
        android:id="@+id/btnMain"
        style="@style/main_menu"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@drawable/red_btn_info"
        android:text="Main Menu" />

</LinearLayout>
4

2 回答 2

0

我不认为在 onCreate() 中创建表是正确的。所以表还没有创建。

您应该在 databasehelper 的不同函数中创建表: /** * create database if it's not existed * * @throws IOException * */ public void createDataBase() {

并确保在调用 createDataBase() 之前打开;

于 2012-07-12T02:44:30.040 回答
0

构建目标 2.1 没有错误。我能够插入数据库,用allContacts()方法检查插入的信息。你提供了最新的代码吗?我没有看到插入接触的实现View.onClickListener()

于 2012-07-12T01:43:05.200 回答