伙计们,我一直试图在android studio中创建的表ive中插入一些数据,这里是代码。
package database.oxbir.com.databae;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Oxbir on 28/10/2018.
*/
public class DataModelLayer extends SQLiteOpenHelper {
public static final String DB_NAME = "oxbir";
public static final int DB_VERSION = 1;
public static final String TBL_NAME = "users";
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_FAMILY = "family";
public static final String COL_AGE = "age";
public static final String Query = "CREATE TABLE IF NOT EXISTS " + TBL_NAME + "(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_NAME + "TEXT," +
COL_FAMILY + "TEXT," +
COL_AGE + "INTEGER);";
Context context;
public DataModelLayer(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try
{
db.execSQL(Query);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long addInfo (String name, String family, int age){
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
contentValues.put(COL_FAMILY, family);
contentValues.put(COL_AGE, age);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
long result = sqLiteDatabase.insert(TBL_NAME, null, contentValues);
if (result > 0)
{
return result;
}
else
{
return result;
}
}
}
package database.oxbir.com.databae;
import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
/**
* Created by Oxbir on 28/10/2018.
*/
public class DataModelLayer extends SQLiteOpenHelper {
public static final String DB_NAME = "oxbir";
public static final int DB_VERSION = 1;
public static final String TBL_NAME = "users";
public static final String COL_ID = "id";
public static final String COL_NAME = "name";
public static final String COL_FAMILY = "family";
public static final String COL_AGE = "age";
public static final String Query = "CREATE TABLE IF NOT EXISTS " + TBL_NAME + "(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_NAME + "TEXT," +
COL_FAMILY + "TEXT," +
COL_AGE + "INTEGER);";
Context context;
public DataModelLayer(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
try
{
db.execSQL(Query);
}
catch (SQLException ex)
{
ex.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public long addInfo (String name, String family, int age){
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
contentValues.put(COL_FAMILY, family);
contentValues.put(COL_AGE, age);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
long result = sqLiteDatabase.insert(TBL_NAME, null, contentValues);
if (result > 0)
{
return result;
}
else
{
return result;
}
}
}
每当我调用此活动时,都会收到id=1
消息。
我发现活动没有任何问题,因此应用程序没有崩溃,所以我决定举杯儿插入查询,看是否有什么问题。
据我所知,只要包括在内,就可以购买。
在我的代码中,应用程序崩溃了(临时包含我刚刚敬酒的查询)
任何帮助将不胜感激。
答案 0 :(得分:1)
如果粘贴了正确的代码,则表的创建sql中有错误,请更改为:
public static final String Query = "CREATE TABLE IF NOT EXISTS " + TBL_NAME + "(" +
COL_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," +
COL_NAME + " TEXT," +
COL_FAMILY + " TEXT," +
COL_AGE + " INTEGER);";
我在字段名称和类型之间包括了空格。
现在,先卸载应用程序,然后再次运行。
还可以简化addInfo()
方法:
public long addInfo (String name, String family, int age){
ContentValues contentValues = new ContentValues();
contentValues.put(COL_NAME, name);
contentValues.put(COL_FAMILY, family);
contentValues.put(COL_AGE, age);
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
long result = sqLiteDatabase.insert(TBL_NAME, null, contentValues);
sqLiteDatabase.close()
return result;
}