简体   繁体   中英

Simple login, signup and show database android app. But login and show database not responding. Getting SQLite Exception

This was a simple login and sign up app i tried to create with an additional feature to show database history. The sign up page works fine. However when I try to login or open database, i get the following error message:-

  Process: com.example.princebhura.hopon, PID: 13301
    android.database.sqlite.SQLiteException: no such column: uid (code 1): , while compiling: Select uid pass, fromcontacts
            at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
            at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:919)
            at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:530)
            at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
            at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
            at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
            at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47)
            at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1379)
            at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1318)
            at com.example.princebhura.hopon.DatabaseHelper.search(DatabaseHelper.java:58)
            at com.example.princebhura.hopon.Login$1.onClick(Login.java:51)
            at android.view.View.performClick(View.java:4785)
            at android.view.View$PerformClick.run(View.java:19858)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

Login.java

  package com.example.princebhura.hopon;

    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;``
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;

    public class Login extends AppCompatActivity {

        private static EditText username;
        private static EditText password;
        private static Button login_button;
        private static Button signup_button;
        private static Button showdatabase_button;
        private static String history;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            System.out.println("hiii");
            Button();
        }
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_login, menu);
            return true;
        }
        DatabaseHelper helper= new DatabaseHelper(this);

        public void Button(){
            username = (EditText)findViewById(R.id.editText);
            final String uidstr=username.getText().toString();
            password = (EditText)findViewById(R.id.editText2);
            final String passwordstr=password.getText().toString();
            login_button = (Button) findViewById(R.id.button);
            signup_button= (Button) findViewById(R.id.button_signup);
            showdatabase_button=(Button)findViewById(R.id.button3);
            //System.out.println(history);
            login_button.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            String pass= helper.search(uidstr);
                            if ( passwordstr.equals(pass)==true){
                                Toast.makeText(Login.this, "Username and password is correct",
                                        Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(Login.this, user.class);
                                startActivity(intent);

                            }
                            else{
                                Toast.makeText(Login.this, "Username and password is NOT correct",
                                        Toast.LENGTH_SHORT).show();

                                Button();
                            }

                        }
                    }
            );
            signup_button.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                                Intent intent = new Intent(Login.this, SignUp.class);
                                startActivity(intent);

                            }

                            });

            showdatabase_button.setOnClickListener(
                    new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {

                            Intent intent = new Intent(Login.this, History.class);
                            history = helper.gethistory();
                            intent.putExtra("Database History:\n", history);
                            startActivity(intent);

                        }

                    }
            );
        }



        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

SignUp package com.example.princebhura.hopon;

    import android.provider.ContactsContract;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.Toast;

    public class SignUp extends AppCompatActivity {

        DatabaseHelper helper = new DatabaseHelper(this);

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_sign_up);

        }

        private static EditText name;
        private static EditText email;
        private static EditText uid;
        private static EditText pass1;
        private static EditText pass2;
        private static Button signup_button1;

        private static String namestr;
        private static String emailstr;
        private static String uidstr;
        private static String pass1str;
        private static String pass2str;


        public void onsignupClick(View v)

        {
            if(v.getId()==R.id.button2) {
                name = (EditText) findViewById(R.id.Name);
                email = (EditText) findViewById(R.id.Email);
                uid = (EditText) findViewById(R.id.Uid);
                pass1 = (EditText) findViewById(R.id.Pass1_text);
                pass2 = (EditText) findViewById(R.id.Pass2_text);
                signup_button1 = (Button) findViewById(R.id.button2);

                namestr = name.getText().toString();
                emailstr = email.getText().toString();
                uidstr = uid.getText().toString();
                pass1str = pass1.getText().toString();
                pass2str = pass2.getText().toString();
               // System.out.println(pass1str + " " + pass2str);

               if (pass1str.equals(pass2str)) {
                    Contact c = new Contact();
                    c.setName(namestr);
                    c.setEmail(emailstr);
                    c.setPassword(pass1str);
                    c.setUid(uidstr);
                    helper.insertContacts(c);
                    Toast temp = Toast.makeText(SignUp.this, "SignUp Complete. Please Login", Toast.LENGTH_SHORT);
                    temp.show();
                }
                else {
                    Toast temp = Toast.makeText(SignUp.this, "Passwords dont match!", Toast.LENGTH_SHORT);
                    temp.show();
                }


            }
        }

            @Override
            public boolean onCreateOptionsMenu (Menu menu){
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_sign_up, menu);
            return true;
        }

            @Override
            public boolean onOptionsItemSelected (MenuItem item){
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
        }

DatabaseHelper

    package com.example.princebhura.hopon;

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

    /**
     * Created by princebhura on 11/02/16.
     */
    public class DatabaseHelper extends SQLiteOpenHelper {
        String TAG= "DatabaseHelper";

        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "contacts.db";
        private static final String TABLE_NAME = "contacts";
        private static final String COLUMN_ID = "id";
        private static final String COLUMN_NAME = "name";
        private static final String COLUMN_EMAIL = "email";
        private static final String COLUMN_UID = "uid";
        private static final String COLUMN_PASS = "pass";
        SQLiteDatabase db;
        private static final String TABLE_CREATE = "create table contacts(id integer primary key not null , " +
                "name text not null, email text not null, uid text not null , pass text not null)";

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

        public void insertContacts(Contact c) {

            db = this.getWritableDatabase();
            ContentValues values = new ContentValues();

            String query="Select * from contacts";
            Cursor cursor=db.rawQuery(query, null);
            int count=cursor.getCount();
            values.put(COLUMN_ID, count);
            values.put(COLUMN_NAME, c.getName());
            values.put(COLUMN_EMAIL, c.getEmail());
            values.put(COLUMN_UID, c.getUid());
            values.put(COLUMN_PASS, c.getPass());

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

        }
        public String gethistory() {
           String hist= getTableAsString(db, TABLE_NAME);
            return hist;
        }

        public String search(String userid) {
            db = this.getReadableDatabase();
            String query = "Select uid pass, from" + TABLE_NAME;
            Cursor cursor=  db.rawQuery(query,null);
            String a, b="Not found#!2334";

            if(cursor.moveToFirst())
            {
                do
                {
                     a=cursor.getString(0);
                    if(a.equals(userid)) {
                        b = cursor.getString(1);
                        break;
                    }

                }
                while (cursor.moveToNext());
            }
            return b;

        }
        public String getTableAsString(SQLiteDatabase db, String tableName) {
            Log.d(TAG, "getTableAsString called");
            String tableString = String.format("Table %s:\n", tableName);
            Cursor allRows  = db.rawQuery("SELECT * FROM " + tableName, null);
            if (allRows.moveToFirst() ){
                String[] columnNames = allRows.getColumnNames();
                do {
                    for (String name: columnNames) {
                        tableString += String.format("%s: %s\n", name,
                                allRows.getString(allRows.getColumnIndex(name)));
                    }
                    tableString += "\n";

                } while (allRows.moveToNext());
            }

            return tableString;
        }

        @Override
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(TABLE_CREATE);
            this.db = db;

        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            String query = "Drop Table if exists" + TABLE_NAME;
            db.execSQL(query);
            this.onCreate(db);

        }
    }

Contact package com.example.princebhura.hopon;

    /**
     * Created by princebhura on 11/02/16.
     */
    public class Contact {
        int id;
        String name, email, uid, pass;

        public void setName(String name)
        {
            this.name=name;
        }
        public String getName()
        {
            return this.name;
        }
        public void setEmail(String email)
        {
            this.email=email;
        }
        public String getEmail()
        {
            return this.email;
        }
        public void setUid(String uid)
        {
            this.uid=uid;
        }
        public String getUid()
        {
            return this.uid;
        }
        public void setPassword(String pass)
        {
            this.pass=pass;
        }
        public String getPass()
        {
            return this.pass;
        }
    }

History

    package com.example.princebhura.hopon;

    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Menu;
    import android.view.MenuItem;

    public class History extends AppCompatActivity {

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_history);
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_history, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();

            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }

            return super.onOptionsItemSelected(item);
        }
    }

mainfest //manifest.xml

        <application
            android:allowBackup="true"
            android:icon="@mipmap/hop"
            android:label="@string/app_name"
            android:theme="@style/AppTheme" >
            <activity
                android:name=".Login"
                android:label="@string/app_name" >
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />

                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
            <activity
                android:name=".user"
                android:label="@string/title_activity_user" >
                <intent-filter>
                    <action android:name="com.example.princebhura.hopon.user" />

                    <category android:name="android.intent.category.default" />
                </intent-filter>
            </activity>
            <activity
                android:name=".SignUp"
                android:label="@string/title_activity_sign_up" >
                <intent-filter>
                    <action android:name="com.example.princebhura.hopon.SignUp" />

                    <category android:name="android.intent.category.default" />
                </intent-filter>
            </activity>
            <activity
                android:name=".History"
                android:label="@string/title_activity_history" >
                <intent-filter>
                    <action android:name="com.example.princebhura.hopon.History" />

                    <category android:name="android.intent.category.default" />
                </intent-filter>
            </activity>
        </application>

    </manifest>

    <RelativeLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:background="@drawable/logo2"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context=".Login"
        android:id="@+id/login_page">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="40sp"
            android:text="@string/appname"
            android:id="@+id/login"
            android:textColorHighlight="#1a04ff"
            android:textColorHint="#1f08ff"
            android:textColorLink="#120aff"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true"
            android:textColor="#010101" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="LOGIN"
            android:id="@+id/button"
            android:theme="@android:style/MediaButton"
            android:textSize="25sp"
            android:layout_above="@+id/button_signup"
            android:layout_centerHorizontal="true" />

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textPassword"
            android:ems="10"
            android:id="@+id/editText"
            android:layout_marginBottom="114dp"
            android:layout_above="@+id/button"
            android:layout_alignLeft="@+id/editText2"
            android:layout_alignStart="@+id/editText2"
            android:layout_marginRight="40dp"
            />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:inputType="textEmailAddress"
            android:ems="10"
            android:layout_marginBottom="20dp"
            android:textColor="@color/abc_search_url_text_normal"
            android:layout_above="@+id/editText"
            android:layout_alignLeft="@+id/login"
            android:layout_alignStart="@+id/login"
            android:layout_marginLeft="27dp"
            android:layout_marginStart="27dp"
            android:layout_marginRight="40dp"
            />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="User id"
            android:id="@+id/textView"
            android:paddingRight="16dp"
            android:layout_alignBottom="@+id/editText2"
            android:layout_toLeftOf="@+id/editText"
            android:layout_toStartOf="@+id/editText"
            android:textColor="#3288ff" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:text="Password"
            android:id="@+id/textView2"
            android:layout_alignBottom="@+id/editText"
            android:layout_alignLeft="@+id/textView"
            android:layout_alignStart="@+id/textView"
            android:paddingRight="16dp"
            android:textColor="#3288ff" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Sign Up"
            android:id="@+id/button_signup"
            android:textSize="19sp"
            android:theme="@android:style/MediaButton"
            android:layout_marginBottom="45dp"
            android:layout_alignParentBottom="true"
            android:layout_alignLeft="@+id/button"
            android:layout_alignStart="@+id/button" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Database"
            android:id="@+id/button3"
            android:layout_alignParentBottom="true"
            android:layout_centerHorizontal="true"
            android:textSize="10sp"
    />
        />

You are getting error on this line String query = "Select uid pass, from" + TABLE_NAME;

I think you want to select uid and pass from table contacts, but your comma had put in the wrong place. Debugger will assume uid pass is a column.

It should be String query = "Select uid, pass FROM" + TABLE_NAME;

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM