用sqlite android插入和解压缩

时间:2015-10-30 03:41:58

标签: android database sqlite insert extract

我试图将数据插入我的数据库,一旦我点击添加按钮,应用程序崩溃。

如何提取数据并将其设置为文本视图,如标签

提前谢谢

这是我的代码

MainActivity类

public class MainActivity extends AppCompatActivity {

DB db;
Button addmed,addpl;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    db = new DB(this);
}

public void addmedView(View view){
    Intent ADDMEDVIEW = new Intent(this,ADDMEDCINEVIEW.class);
    startActivity(ADDMEDVIEW);

}
public void addpplview(View view){
    Intent ADDPPLVIEWS = new Intent(this,ADDPPLVIEW.class);
    startActivity(ADDPPLVIEWS);
}}

ADDMEDCINEVIEW我试图插入数据的类

public class ADDMEDCINEVIEW extends Activity {
DB db;
EditText MEDNAME,MEDPORP,NOT;
Button ADDDATA;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.addmedview);
    MEDNAME = (EditText)findViewById(R.id.mednamevalue);
    MEDPORP = (EditText)findViewById(R.id.purposevalue);
    NOT = (EditText)findViewById(R.id.nooftapvalue);
    ADDDATA = (Button)findViewById(R.id.ADDMEDDATA);
    addDATA();
}

public void addDATA(){
    ADDDATA.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            boolean isInserted = db.inserData(
                    MEDNAME.getText().toString(),
                    MEDPORP.getText().toString(),
                    NOT.getText().toString()
            );
            if(isInserted == true){
                Toast.makeText(ADDMEDCINEVIEW.this,"Inserted",Toast.LENGTH_LONG).show();
            }
            else
                Toast.makeText(ADDMEDCINEVIEW.this,"NOT INSERTED",Toast.LENGTH_LONG).show();
        }
    });

}}

DataBase Class

public class DB extends SQLiteOpenHelper {

public final static String DBNAME="MEDCINEDB.db";
public final static String Table_name="MEDCINETable";
public final static String col1="MEDCINEID";
public final static String col2="MEDCINENAME";
public final static String col3="MEDCINEPURPOSE";
public final static String col4="NOTAPLET";


public DB(Context context) {
    super(context, DBNAME, null, 1);

}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("CREATE TABLE " + Table_name + "(MEDCINEID INTEGER PRIMARY KEY AUTOINCREMENT,MEDCINENAME TEXT,MEDCINEPURPOSE TEXT,NOTAPLET INTEGER)");
}

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

}
public boolean inserData(String MEDCINENAME,String MEDCINEPURPOSE,String NOTAPLET){
    SQLiteDatabase db= this.getWritableDatabase();
    ContentValues contentValues = new ContentValues();
    contentValues.put(col2,MEDCINENAME);
    contentValues.put(col3,MEDCINEPURPOSE);
    contentValues.put(col4,NOTAPLET);

    long Result = db.insert(Table_name,null ,contentValues);
    if(Result == -1){
        return false;
    }
    else
        return true;
}}

清单

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

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".ADDMEDCINEVIEW"></activity>
    <activity android:name=".ADDPPLVIEW"></activity>
</application>

1 个答案:

答案 0 :(得分:1)

所以它说了所有内容,你还没有初始化你的db对象,你刚刚声明了它。 将其初始化为活动db=new DB(ADDMEDCINEVIEW.this)中的onCreate