数据库助手无法创建数据库

时间:2015-11-30 11:49:51

标签: java android database sqlite android-sqlite

我是android的初学者。

我无法使用我的数据库助手成功插入数据库吗?

这是我的助手类代码,其中包含插入和查找用户的方法

package com.example.tilmac.dbsql;

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

/**
 * Created by TILMAC on 28-11-15.
 */
public class DatabaseHelper extends SQLiteOpenHelper {

    private static final int DATABASE_VERSION = 1;
    private static final String DATABASE_NAME = "User.db";
    private static final String TABLE_NAME = "users";       //final variables
    private static final String COLUMN_NAME = "name";
    private static final String COLUMN_EMAIL = "email";
    private static final String COLUMN_UNAME = "uname";
    private static final String COLUMN_PASS = "pass";
    SQLiteDatabase db;


    public DatabaseHelper(Context context)              //Constructor
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

创建表语句是硬编码的,如下所示:

  private static final String TABLE_CREATE = "create table users (name text not null, email text not null, uname text not null, pass text not null)";

我收到一条错误,上面写着column not found while inserting into database

    public void insertUser(Contact c){
        db = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        values.put(COLUMN_NAME, c.getName());
        values.put(COLUMN_EMAIL, c.getEmail());
        values.put(COLUMN_UNAME, c.getUname());
        values.put(COLUMN_PASS, c.getPass());

        db.insert(TABLE_NAME, null, values);
        db.close();

    }

    public String findPass(String s){
        db = this.getReadableDatabase();
        String query = "select uname, pass from "+TABLE_NAME;
        Cursor cursor = db.rawQuery(query, null);
        String a, b="not found";

        if(cursor.moveToFirst())
        {
            do{
                a = cursor.getString(0);
                if(a.equals(s))
                {
                    b=cursor.getString(1);
                    break;
                }
            }while(cursor.moveToNext());
        }
        return b;
    }

    @Override
    public void onCreate(SQLiteDatabase db) {                               //OnCreate method

        db.execSQL(TABLE_CREATE);
        this.db = db;
    }

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

        String query = "DROP TABLE IF EXISTS " + TABLE_NAME;
        db.execSQL(query);
        this.onCreate(db);
    }
}

//and here is my main activity

package com.example.tilmac.dbsql;

import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by TILMAC on 27-11-15.
 */
public class RegisterActivity extends AppCompatActivity {

    DatabaseHelper helper = new DatabaseHelper(this);
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ///Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //setSupportActionBar(toolbar);
    }

    public void onRBClick(View v){
        if(v.getId()==R.id.Rbutton)
        {
            EditText n = (EditText)findViewById(R.id.RETname);
            EditText e = (EditText)findViewById(R.id.RETemail);
            EditText u = (EditText)findViewById(R.id.RETuname);
            EditText p1 = (EditText)findViewById(R.id.RETpass);
            EditText p2 = (EditText)findViewById(R.id.RETrepass);

            String name, email, uname, pass, repass;
            name = n.getText().toString();
            email = e.getText().toString();
            uname = u.getText().toString();
            pass = p1.getText().toString();
            repass = p2.getText().toString();

            if(pass.equals(repass))
            {
                Contact c = new Contact();
                c.setName(name);
                c.setEmail(email);
                c.setUname(uname);
                c.setPass(pass);

                helper.insertUser(c);

            }
            else
            {
                Context context = getApplicationContext();
                Toast toast = Toast.makeText(context, "Passwords dont match", Toast.LENGTH_SHORT);
                toast.show();
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您在上一次运行后添加了列 只需卸载并重新安装您的应用。