Sqlite插入值不工作

时间:2018-04-16 07:08:50

标签: java android html

您好我的数据库SQLite android有问题。

请查看我的代码以供参考

当我要插入值时,它返回-1,我从调试中得知-1,它没有插入值,所以告诉我有什么问题

这是我的创建表方法:

public void onCreate(SQLiteDatabase db) {
        db.execSQL(
                "create table user_table " +
                        "(id integer primary key, name text,phone text,question text)"
        );
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS user_table");
        onCreate(db);
    }

这是插入方法:

public int insertContact (String name, String phone, String question) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put("name", name);
        contentValues.put("phone", phone);
        contentValues.put("question", question);
        Long result = db.insert("contacts", null, contentValues);
        if(result == -1)
        {
            return 1;
        }
        else
            return 2;
    }

这是我调用方法的地方:

submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               int s =  mydb.insertContact(name.getText().toString(),phone.getText().toString(),question.getText().toString());
               if(s ==1)
               {
                   Toast.makeText(MainActivity.this, "fail", Toast.LENGTH_SHORT).show();
               }
               else
                   Toast.makeText(MainActivity.this, "success", Toast.LENGTH_SHORT).show();

            }
        });

4 个答案:

答案 0 :(得分:0)

您正在创建一个名为user_table的表,但是在名为contacts的表中输入值,该表实际上不存在。

更改此行

  Long result = db.insert("contacts", null, contentValues);

to,

 Long result = db.insert("user_table ", null, contentValues);

答案 1 :(得分:0)

在创建数据并向其中插入数据时,您需要为表提供相同的名称。

请改变它

  "create table user_table " +
                        "(id integer primary key, name text,phone text,question text)"

 "create table contacts " +
                        "(id integer primary key, name text,phone text,question text)"

或者如果您想在user_table中添加数据,请更改

Long result = db.insert("user_table", null, contentValues);

答案 2 :(得分:0)

使用以下内容更改insertContact方法:

public int insertContact (String name, String phone, String question) {
            SQLiteDatabase db = this.getWritableDatabase();
            ContentValues contentValues = new ContentValues();
            contentValues.put("name", name);
            contentValues.put("phone", phone);
            contentValues.put("question", question);
            Long result = db.insert("user_table", null, contentValues);
            if(result == -1)
            {
                return 1;
            }
            else
                return 2;
        }

答案 3 :(得分:0)

更改您的表名

# JavaScript für Google Analytics

temp.google.analytics = TEXT
temp.google.analytics.value (

<script>
  (function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
  (i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
  m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
  })(window,document,'script','//www.google-analytics.com/analytics.js','ga');

  ga('create', 'UA-87654123-1', 'auto');
  ga('send', 'pageview');

</script>

)