【问题标题】:Am unable to redirect to previous activity in Android Studio我无法重定向到 Android Studio 中的先前活动
【发布时间】:2018-04-28 12:02:03
【问题描述】:

我无法将当前活动重定向到 Android Studio 中的主要活动。我试图将注册活动的数据插入数据库 sqLite,但它不起作用。

b2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String s1 = e1.getText().toString();
            String s2 = e2.getText().toString();
            String s3 = e3.getText().toString();
            String s6 = e6.getText().toString();
            if (s1.equals("") || s2.equals("") || s3.equals("") || s6.equals("")) {
                Toast.makeText(Second.this, "FILL ALL THE FIELDS", Toast.LENGTH_SHORT).show();
            } else {
                SQLiteDatabase data = openOrCreateDatabase("db1", MODE_PRIVATE, null);
                data.execSQL("CREATE TABLE IF NOT EXISTS users1(name varchar,email varchar,password varchar,phone varchar)");
                String s7 = "SELECT * FROM users1 WHERE name='" + s1 + "' and email='" + s2 + "' and password='" + s3 + "'";
                Cursor cursor = data.rawQuery(s7, null);
                if (cursor.getCount() > 0) {
                    Toast.makeText(Second.this, "already exists", Toast.LENGTH_SHORT).show();
                } else {
                    data.execSQL("insert into users1 values('" + s1 + "','" + s2 + "','" + s3 + "','" + s6 + "')");
                    Toast.makeText(Second.this, "all field filled", Toast.LENGTH_SHORT).show();
                    Intent il = new Intent(Second.this, MainActivity.class);
                    startActivity(il);
                    finish();

                }
            }
        }
    });

按要求记录。

    04-28 18:09:44.150 26440-26440/com.example.abhinav.datal E/SQLiteLog: (1) table users1 has 3 columns but 4 values were supplied
04-28 18:09:44.150 26440-26440/com.example.abhinav.datal D/AndroidRuntime: Shutting down VM
04-28 18:09:44.151 26440-26440/com.example.abhinav.datal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                           Process: com.example.abhinav.datal, PID: 26440
                                                                           android.database.sqlite.SQLiteException: table users1 has 3 columns but 4 values were supplied (code 1): , while compiling: INSERT INTO users1 VALUES('aa','aa','aa','44')
                                                                               at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                               at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:896)
                                                                               at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:507)
                                                                               at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                               at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                               at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
                                                                               at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1704)
                                                                               at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1635)
                                                                               at com.example.abhinav.datal.Second$1.onClick(Second.java:49)
                                                                               at android.view.View.performClick(View.java:5269)
                                                                               at android.view.View$PerformClick.run(View.java:21556)
                                                                               at android.os.Handler.handleCallback(Handler.java:815)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                               at android.os.Looper.loop(Looper.java:207)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:5776)
                                                                               at java.lang.reflect.Method.invoke(Native Method)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
04-28 18:09:49.925 26440-26449/com.example.abhinav.datal I/System: FinalizerDaemon: finalize objects = 126

【问题讨论】:

  • 你能分享你的日志吗?
  • 我已经在答案中发布了日志,请检查。

标签: java android database sqlite android-studio


【解决方案1】:

由于日志提示您的表 users1 具有 3 列,并且您正尝试将 4 值插入其中。

以下是场景:-

1 -您确实需要3 列,但您错误地插入了4 元素。

2 -您想要4 列,但您的表users13 列并且您正在编写此查询

SQLiteDatabase data = openOrCreateDatabase("db1", MODE_PRIVATE, null);
                data.execSQL("CREATE TABLE IF NOT EXISTS users1(name varchar,email varchar,password varchar,phone varchar)");

如果users1 不存在,此查询将创建四列表。 如果users1 表存在,则不会创建任何表。 因此,您所要做的就是首先删除在您的设备中创建的架构。为此,您必须从您的设备中卸载现有应用程序并尝试重新安装它。

【讨论】:

    【解决方案2】:

    您希望使用以下语句创建表:

    data.execSQL("CREATE TABLE IF NOT EXISTS users1(name varchar,email varchar,password varchar,phone varchar)");
    

    例外状态:

    [...] table users1 has 3 columns but 4 values were supplied [...]
    

    如果您之前创建的表 user1 只有 3 列,您的代码将不会更新它,因为它会检查表是否已经存在 (CREATE TABLE IF NOT EXISTS)。

    您可以尝试删除或更改表 user1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-05-13
      • 2021-11-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-13
      • 1970-01-01
      • 2016-01-05
      相关资源
      最近更新 更多