无法在Android中的Sqlite数据库中保存数据?

时间:2017-02-25 14:54:41

标签: android sqlite

我已经按照以下方式编写了DB Helper类,并且面临将数据插入表格的问题。

这是我的DB助手:

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


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

/**
 * Created by Admin on 2/24/2017.
 */
public class CompanyDatabaseHandler extends SQLiteOpenHelper {

    // All Static variables
    // Database Version
    private static final int DATABASE_VERSION = 5;

    // Database Name
    private static final String DATABASE_NAME = "databaseofcompany";

    // COMPANY table name
    private static final String TABLE_COMPANY = "detailsofcompany";
    // COMPANY Table Columns names
    private static final  String KEY_ID="_id";
    private static final String KEY_COMPANYNAME="_companyName";
    private static final String KEY_COMPANYURL="_companyUrl";
    private static final String KEY_COMPANYEMAIL="_companyEmail";
    private static final String KEY_COMPANYNUMMBER="_companyNumber";
    private static final String KEY_COMPANYADDRESS="_companyAddress";

    public CompanyDatabaseHandler(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }


/*
Creating new Table
 */
    @Override
    public void onCreate(SQLiteDatabase db) {

        String CREATE_COMPANY_TABLE = "CREATE TABLE "
                + TABLE_COMPANY + "("+ KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_COMPANYNAME + " TEXT,"+ KEY_COMPANYURL + " TEXT,"+ KEY_COMPANYEMAIL + " TEXT"+ KEY_COMPANYNUMMBER+" TEXT"+ KEY_COMPANYADDRESS+" TEXT"+")";
        db.execSQL(CREATE_COMPANY_TABLE);
    }
/*
Upgrading Table
 */
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS " + TABLE_COMPANY);

        // Create tables again
        onCreate(db);
    }

    // Adding new company
    public void addCompany(Company company) {
        SQLiteDatabase db = CompanyDatabaseHandler.this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_COMPANYNAME, company.getCompanyName());
        values.put(KEY_COMPANYURL, company.getCompanyLink());
        values.put(KEY_COMPANYEMAIL, company.getCompanyEmail());
        values.put(KEY_COMPANYNUMMBER, company.getCompanyNumber());
        values.put(KEY_COMPANYADDRESS, company.getCompanyAddress());

        // Inserting Row
        db.insert(TABLE_COMPANY, null, values);
        // Closing database connection
        db.close();
    }

    // Getting single company
    public Company getCompany(int id) {SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.query(TABLE_COMPANY,                                         //Table
                        new String[] { KEY_COMPANYNAME,KEY_COMPANYURL,KEY_COMPANYEMAIL ,KEY_COMPANYNUMMBER,KEY_COMPANYADDRESS}, //Colunms
                        KEY_ID + "=?",       //Selection KEY
                        new String[] { String.valueOf(id) },//Selection Args
                        null,                //Group By
                        null,                //Having
                        null);                //Order By


        if (cursor != null)
            cursor.moveToFirst();

        Company company=new Company(Integer.parseInt(cursor.getString(0)),cursor.getString(1),cursor.getString(2),cursor.getString(3),cursor.getString(4),cursor.getString(5));
        // return company
        return company;
    }

    // Getting All Company
    public List<Company> getAllCompany() {
        List<Company> companyList = new ArrayList<Company>();
        // Select All Query
        String selectQuery = "SELECT * FROM " + TABLE_COMPANY;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Company company = new Company();
                company.setCompanyId(Integer.parseInt(cursor.getString(0)));
                company.setCompanyName(cursor.getString(1));
                company.setCompanyLink(cursor.getString(2));
                company.setCompanyEmail(cursor.getString(3));
                company.setCompanyNumber(cursor.getString(4));
                company.setCompanyAddress(cursor.getString(5));
                // Adding company to list
                companyList.add(company);
            } while (cursor.moveToNext());
        }

        // return companyList
        return companyList;
    }

    // Getting company Count
    public int getCompanyCount() {
        String countQuery = "SELECT * FROM " + TABLE_COMPANY;
        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(countQuery, null);
        int count=cursor.getCount();
        cursor.close();

         return count;
//        return cursor.getCount();
    }

    // Updating single Company
    public int updateCompany(Company company) {
        SQLiteDatabase db = this.getWritableDatabase();

        ContentValues values = new ContentValues();
        values.put(KEY_COMPANYNAME, company.getCompanyName());
        values.put(KEY_COMPANYURL, company.getCompanyLink());
        values.put(KEY_COMPANYEMAIL, company.getCompanyEmail());
        values.put(KEY_COMPANYNUMMBER, company.getCompanyNumber());
        values.put(KEY_COMPANYADDRESS, company.getCompanyAddress());
        // updating row
        return db.update(TABLE_COMPANY, values, KEY_ID + " = ?",
                new String[] { String.valueOf(company.getCompanyId()) });
    }

    // Deleting single company
    public void deleteCompany(Company company) {
        SQLiteDatabase db = this.getWritableDatabase();
        db.delete(TABLE_COMPANY, KEY_ID + " = ?",
                new String[] { String.valueOf(company.getCompanyId()) });
        db.close();
    }
}

但是在尝试添加表格时,我得到如下:

    table detailsofcompany has no column named _companyAddress
02-25 20:12:29.781 19919-19919/############ E/SQLiteDatabase: Error inserting _companyAddress=Address here _companyEmail=xyz@abc.com _companyNumber=09876543210 _companyUrl=http://www.google.com _companyName=ABC COMPANY
                                                                                          android.database.sqlite.SQLiteException: table detailsofcompany has no column named _companyAddress (code 1): , while compiling: INSERT INTO detailsofcompany(_companyAddress,_companyEmail,_companyNumber,_companyUrl,_companyName) VALUES (?,?,?,?,?)

我是android SQLITE实现的新手。我已经尝试更新版本和更改数据库名称以及列名称,但它没有帮助。

我被困了你的建议会很有帮助。我不知道我哪里错了。

1 个答案:

答案 0 :(得分:2)

CREATE_COMPANY_TABLE字符串中缺少逗号。对于两列,您在TEXT之后错过了逗号。在运行已完成更改的应用程序之前,还要卸载现有应用程序。

@Override
public void onCreate(SQLiteDatabase db) {

    String CREATE_COMPANY_TABLE = "CREATE TABLE "
            + TABLE_COMPANY + "("+ KEY_ID + " INTEGER PRIMARY KEY,"+ KEY_COMPANYNAME + " TEXT,"+ KEY_COMPANYURL + " TEXT,"+ KEY_COMPANYEMAIL + " TEXT,"+ KEY_COMPANYNUMMBER+" TEXT,"+ KEY_COMPANYADDRESS+" TEXT"+")";
    db.execSQL(CREATE_COMPANY_TABLE);
}