Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
153 views
in Technique[技术] by (71.8m points)

android - App stopped working due to database

I am creating a signup page, when I enter different passwords, toast is working just fine. But when I enter same passwords. MainActivity is not being redirected to a yaya Activity . It just crashes with a message "app stopped working" and I'm redirected to my main activity again.

package com.example.abcd.helloworld;
import....

public class SignUp extends Activity {
DatabaseHelper helper = new DatabaseHelper(this);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.signup);
    Button button2 = (Button) findViewById(R.id.Bsignupbutton);

    button2.setOnClickListener(new View.OnClickListener() {

        @Override

        public void onClick(View v) {
            if (v.getId() == R.id.Bsignupbutton) {
                EditText name = (EditText) findViewById(R.id.TFname);
                EditText email = (EditText) findViewById(R.id.email);
                EditText uname = (EditText) findViewById(R.id.TFuname);
                EditText pass1 = (EditText) findViewById(R.id.password);
                EditText pass2 = (EditText) findViewById(R.id.pass2);

                String namestr = name.getText().toString();
                String emailstr = email.getText().toString();
                String unamestr = uname.getText().toString();
                String pass1str = pass1.getText().toString();
                String pass2str = pass2.getText().toString();
                if (!pass1str.equals(pass2str)) {
                    //popup msg
                    Toast pass = Toast.makeText(SignUp.this, "Passwords do not match!", Toast.LENGTH_SHORT);
                    pass.show();
                } else {
                    User u = new User();
                    u.setName(namestr);
                    u.setEmail(emailstr);
                    u.setUname(unamestr);
                    u.setPass(pass1str);

                    helper.insertUser(u);
                    Intent i = new Intent(SignUp.this, yaya.class);
                    startActivity(i);
                }

            }
        }
    });
}
}

errors are

02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: FATAL EXCEPTION: main
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: android.database.sqlite.SQLiteException: near "mull": syntax error (code 1): , while compiling: create table contacts (id integer primary key not mull auto_increment,name text not null, email text not null, uname text not null, pass text not null
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at com.example.sanchit.helloworld.DatabaseHelper.onCreate(DatabaseHelper.java:30)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at com.example.sanchit.helloworld.DatabaseHelper.insertUser(DatabaseHelper.java:36)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at com.example.sanchit.helloworld.SignUp$1.onClick(SignUp.java:51)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.view.View.performClick(View.java:4240)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:17721)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:730)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:137)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:525)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

DatabaseHelper.java package com.example.abcd.helloworld;

import...


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="user";
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_UNAME="uname";
private static final String COLUMN_PASS="pass";
SQLiteDatabase db;
private static final String TABLE_CREATE="create table contacts (id integer primary key not null autoincrement,"+
        "name text not null, email text not null, uname text not null, pass text not null );";
public DatabaseHelper(Context context)
{
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
}

public void insertUser(User u)
{
    db = this.getWritableDatabase();
    ContentValues values=new ContentValues();

    String query="select * from user";
    Cursor cursor= db.rawQuery(query, null);
    int count= cursor.getCount();
    values.put(COLUMN_ID, count);
    values.put(COLUMN_NAME, u.getName());
    values.put(COLUMN_EMAIL, u.getEmail());
    values.put(COLUMN_UNAME, u.getUname());
    values.put(COLUMN_PASS, u.getPass());

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

public String searchPass(String uname)
{
    db=this.getReadableDatabase();
    String query="select uname, pass from" +TABLE_NAME;
    Cursor cursor= db.rawQuery(query,null);
    String a,b;
    b="not found";
    if (cursor.moveToFirst())
    {
        do {
            a= cursor.getString(0);

            if (a.equals(uname))
            {
                b= cursor.getString(1);
                break;
            }
        }
        while(cursor.moveToNext());
    }
    db.close();
    return b;

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   String query= "DROP TABLE IF EXISTS"+ TABLE_NAME;
    db.execSQL(query);
    this.OnCreate(db);
}

private void OnCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    this.db = db;
}

}

SQLiteProgram.java

package android.database.sqlite;

import android.database.DatabaseUtils;
import android.os.CancellationSignal;

import java.util.Arrays;

public abstract class SQLiteProgram extends SQLiteClosable {
private static final String[] EMPTY_STRING_ARRAY = new String[0];

private final SQLiteDatabase mDatabase;
private final String mSql;
private final boolean mReadOnly;
private final String[] mColumnNames;
private final int mNumParameters;
private final Object[] mBindArgs;

SQLiteProgram(SQLiteDatabase db, String sql, Object[] bindArgs,
        CancellationSignal cancellationSignalForPrepare) {
    mDatabase = db;
    mSql = sql.trim();

    int n = DatabaseUtils.getSqlStatementType(mSql);
    switch (n) {
        case DatabaseUtils.STATEMENT_BEGIN:
        case DatabaseUtils.STATEMENT_COMMIT:
        case DatabaseUtils.STATEMENT_ABORT:
            mReadOnly = false;
            mColumnNames = EMPTY_STRING_ARRAY;
            mNumParameters = 0;
            break;

        default:
            boolean assumeReadOnly = (n == DatabaseUtils.STATEMENT_SELECT);
            SQLiteStatementInfo info = new SQLiteStatementInfo();
            db.getThreadSession().prepare(mSql,db.getThreadDefaultConnectionFlags(assumeReadOnly),cancellationSignalForPrepare, info);
            mReadOnly = info.readOnly;
            mColumnNames = info.columnNames;
            mNumParameters = info.numParameters;
            break;
    }

    if (bindArgs != null && bindArgs.length > mNumParameters) {
        throw new IllegalArgumentException("Too many bind arguments.  "
                + bindArgs.length + " arguments were provided but the statement needs "
                + mNumParameters + " arguments.");
    }

    if (mNumParameters != 0) {
        mBindArgs = new Object[mNumParameters];
        if (bindArgs != null) {
            System.arraycopy(bindArgs, 0, mBindArgs, 0, bindArgs.length);
        }
    } else {
        mBindArgs = null;
    }
}

final SQLiteDatabase getDatabase() {
    return mDatabase;
}

final String getSql() {
    return mSql;
}

final Object[] getBindArgs() {
    return mBindArgs;
}

final String[] getColumnNames() {
    return mColumnNames;
}

/** @hide */
protected final SQLiteSession getSession() {
    return mDatabase.getThreadSession();
}

/** @hide */
protected final int getConnectionFlags() {
    return mDatabase.getThreadDefaultConnectionFlags(mReadOnly);
}

/** @hide */
protected final void onCorruption() {
    mDatabase.onCorruption();
}

/**
 * Unimplemented.
 * @deprecated This method is deprecated and must not be used.
 */
@Deprecated
public final int getUniqueId() {
    return -1;
}


public void bindNull(int index) {
    bind(index, null);
}

/**
 * Bind a long value to this statement. The value remains bound until
 * {@link #clearBindings} is called.
 *addToBindArgs
 * @param index The 1-based index to the parameter to bind
 * @param value The value to bind
 */
public void bindLong(int index, long value) {
    bind(index, value);
}

/**
 * Bind a double value to this statement. The value remains bound until
 * {@link #clearBindings} is called.
 *
 * @param index The 1-based index to the parameter to bind
 * @param value The value to bind
 */
public void bindDouble(int index, double value) {
    bind(index, value);
}

/**
 * Bind a String value to this statement. The value remains bound until
 * {@link #clearBindings} is called.
 *
 * @param index The 1-based index to the parameter to bind
 * @param value The value to bind, must not be null
 */
public void bindString(int index, String value) {
    if (value == null) {
        throw new IllegalArgumentException("the bind value at index " + index + " is null");
    }
    bind(index, value);
}

/**
 * Bind a byte array value to this statement. The value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
android.database.sqlite.SQLiteException: near "mull": syntax error (code 1): , while compiling: create table contacts (id integer primary key not mull auto_increment

The correct declaration for an autoincrement PK column is integer primary key autoincrement, not integer primary key not mull auto_increment. You have a typo in null and the not null is not required. There is no underscore in sqlite autoincrement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...